MGLP Posted December 15, 2018 Share Posted December 15, 2018 I have a button with onclick calling a function but nothing happens - no execution, no error msgs, just nothing but every thing else works perfectly. My code is: function goBack() {location.href=\"FT_Tree.php\"; return false; window.open(location.href); return false;} <input type=\"Button\" value=\"Home\" class=\"noprint\" style=\"background-color:lightgrey;height:30;width:150\" onclick=\"goBack()\"> Link to comment Share on other sites More sharing options...
Ingolme Posted December 15, 2018 Share Posted December 15, 2018 All those backslashes are probably going to cause a problem. Other problems are that no code will run after a return statement and the browser will leave the page as soon as you assign a value to location.href. This button could easily be replace with a link and would not need any Javascript at all. Link to comment Share on other sites More sharing options...
MGLP Posted December 15, 2018 Author Share Posted December 15, 2018 I've been using this type of code in loads of files / websites and it works perfectly, e.g. below from another of my files works fine. About the backslashes, I need to put those in for the quotes. Otherwise I get and error. If I remember correctly, it's PHP requirement or is it Java? Don't know but it works with backslash and it doesn't work without backslash. function mycontacts() {location.href=\"FT_Contacts.php\"; return false; window.open(location.href); return false;} <input type=\"button\" value=\"Contacts\" onclick=\"mycontacts()\"> Link to comment Share on other sites More sharing options...
dsonesuk Posted December 16, 2018 Share Posted December 16, 2018 The reason the JavaScript code works is because it never gets to the second line, the first is always carried out! Does it ever! Open a new window or tab? which is what the second line would do. IF it is PHP echo "<input type=\"Button\" value=\"Home\" class=\"noprint\" style=\"background-color:lightgrey;height:30;width:150\" onclick=\"goBack()\">"; OR JavaScript .innerHTML="<input type=\"Button\" value=\"Home\" class=\"noprint\" style=\"background-color:lightgrey;height:30;width:150\" onclick=\"goBack()\">"; Then yes! these are required, but! If you use single quotes instead of double quotes to encapsulate the html you can avoid the slashes all together. echo '<input type="Button" value="Home" class="noprint" style="background-color:lightgrey;height:30;width:150" onclick="goBack()">'; innerHTML='<input type="Button" value="Home" class="noprint" style="background-color:lightgrey;height:30;width:150" onclick="goBack()">'; Link to comment Share on other sites More sharing options...
MGLP Posted December 16, 2018 Author Share Posted December 16, 2018 Quote My original problem was resolved. I don't know exactly what the problem was but obviously it was some error in my preceding code. I did some cleaning of my preceding code and then everything was OK. Concerning the NOT opening of a new window, you are absolutely correct. A new window is not being opened. So I will amend my code accordingly. Many thanks for the tip on using the single quote instead of backslash double quote. I've tried it and it works just fine as you said. Actually, many thank for everything. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now