Javascript onclick = "location.replace ('url')" does not work

I am writing some inline javascript. it doesn't work, and I'm not sure why.

at the top of the index page

<meta http-equiv="Content-Script-Type" content="text/javascript" />

later I have:

 <select name='id'> <option value=-1>New Entry</option> <option value='1' onclick="location.replace('index.php?page=update&id=1')">2010-06-12 16:38:08</option> <option value='2' onclick="location.replace('index.php?page=update&id=2')">2010-06-12 18:20:49</option> <option value='3' onclick="location.replace('index.php?page=update&id=3')">2010-06-13 11:39:09</option> </select> 

I want the page to be replaced when one of the options was selected, but the code does not cause the page to refresh, and I'm not sure why. is something wrong with javascript?

+4
source share
2 answers

Use the onchange select element event:

 <select name="id" onchange="window.location.replace('index.php?page=update&id='+this.options[this.selectedIndex].value);"> <option value="-1">New Entry</option> <option value="1">2010-06-12 16:38:08</option> <option value="2">2010-06-12 18:20:49</option> <option value="3">2010-06-13 11:39:09</option> </select> 

Note. The location.replace method is used when you want to go to the page and also replace the current page in the browsing history. If you just want to go to the page normally, assign the URL to the window.location.href property instead.

+4
source

I found that window.location.href does not work at all in new browsers (FF4, Chrome 10, etc.).

+2
source

Source: https://habr.com/ru/post/1313271/


All Articles