Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change base href using javascript

Can I change the href by javascript?

I use the following code in but it doesn't work:

<base href="/" />
<script type="text/javascript"> 
function setbasehref(basehref) { 
 var thebase = document.getElementsByTagName("base"); 
 thebase[0].href = basehref; 
} 
//setbasehref("/");
setbasehref("http://newurl.com");
</script> 
like image 539
Billy Avatar asked Aug 31 '25 06:08

Billy


1 Answers

This line:

document.getElementsByTagName("base"); 

Do you have an element of type "base"?

Perhaps you could try grabbing the element by its ID attribute?

 document.getElementById("YourHyperlinksID"); 

Good luck

like image 153
Paul Avatar answered Sep 02 '25 20:09

Paul