html - How to change href attribute using JavaScript after opening the link in a new window? -


i have link on page

<a href="http://google.com" id="mylink" onclick="changelink();" target="_blank">google</a> 

and goal follow link (opening in new tab) , change attributes (on previous tab). mean open google.com in new tab , if on link, it's refreshed.

i've tried js code

function changelink(){     document.getelementbyid("mylink").href = "http://facebook.com";     document.getelementbyid("mylink").innerhtml = "facebook";     } 

but changes target of new opening tab. instead of opening google opens facebook in example.

is possible fix it? in advance.

your onclick fires before href change before page opened, need make function handle window opening so:

function changelink() {     var link = document.getelementbyid("mylink");      window.open(       link.href,       '_blank'     );      link.innerhtml = "facebook";,     link.setattribute('href', "http://facebook.com");      return false; } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -