Replacing ahref with js | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Replacing ahref with js

Hi, what would be the disadvantages of using javascript to change the content of the page instead of using a links to navigate to different pages? would the page load slower? or for example using javascript to change the css scripts which in turn changes the style and content. this would stop the script from becoming saturated with data while also save loading time by not diverting the user to another page?

27th Aug 2017, 3:20 AM
Michael Bosher
Michael Bosher - avatar
2 Answers
+ 1
Without having a href, the click will reload the current page, so you need something like this: <a href="#" onclick="f1()">jhhghj</a> Or prevent the scroll like this: <a href="#" onclick="f1(); return false;">jhhghj</a> Or return false in your f1 function and: <a href="#" onclick="return f1();">jhhghj</a> ....or, the unobtrusive way: <a href="#" id="abc">jhg</a> <a href="#" id="myLink">jhhghj</a> <script type="text/javascript"> document.getElementById("myLink").onclick = function() { document.getElementById("abc").href="xyz.php"; return false; }; </script>
27th Aug 2017, 5:23 AM
Dor Nachemani
Dor Nachemani - avatar
+ 1
thanks. i was going to use buttons and functions that use the getELment function in my nav and do away with links altogether. I thought if i m nit traversing pages tgen the back button might not work in the browser so i was gonna get around this by storing the getelment data used to viee the pages in arrays and then make a back button so the user can go back to the previous article
27th Aug 2017, 6:31 AM
Michael Bosher
Michael Bosher - avatar