简体   繁体   中英

Javascript functions not working when called

I defined 2 javascript functions in a php file

<script type="javascript">
function inviaRichiesta(){
    //some data elaborations
    location.href = '../index.php'; //page redirect       
}

function caricaDati(){
    var ele = document.getElementById("int").options[document.getElementById("int").selectedIndex].text;
    document.getElementById("tip").value = <?php echo $_SESSION['caricato']['tipologia']; ?>
}
</script>

but none of them seems to work when i change the value inside the select menu

  <select name="intervento" onchange="inviaRichiesta()" id="int">

what i need is to redirect the browser to a page when a choise in the select menu is made. any clue?

Console output:

> [12:09:07.771] ReferenceError: inviaRichiesta is not defined @
> http://localhost/tirocinio/view/inserimento_dati_intervento.php:1

Solution:

For redirecting to some other page you should not use location.href you should use "window.location". Just replace and try.

You can try this as well,

onchange="window.location='../index.php'"

location.href - The href property returns the entire URL of the current page.

window.location - The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.

Reference: http://www.w3schools.com/jsref/prop_loc_href.asp

http://www.w3schools.com/js/js_window_location.asp

Demo

on change page is redirected to URL

window.location.assign("http://www.w3schools.com");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM