OM2 Posted July 29, 2008 Share Posted July 29, 2008 I've got a button. When the button is clicked, I want to go to a specific page. So far I have: function goBack() { window.open('http://www.google.com'); } And: <input type="button" class="clickexecute" value="Return" onclick="goBack()"> Works OK: apart from the fact it opens in a new window! How do I create a link on a button as described above and make it stay in the same window. NOTE: I don't want to use a form. I know how to do it using a form. Thanks. OM Quote Link to comment Share on other sites More sharing options...
secoxxx Posted July 29, 2008 Share Posted July 29, 2008 why wouldnt you just use a link? Quote Link to comment Share on other sites More sharing options...
OM2 Posted July 29, 2008 Author Share Posted July 29, 2008 well... it has to fit in wit what's already there. in the application i have programmed, there are 2 buttons without forms and both have javascript functions associated that control their actions. i can do, but it would look odd to have a link next to 2 buttons. better to have a button. Quote Link to comment Share on other sites More sharing options...
secoxxx Posted July 29, 2008 Share Posted July 29, 2008 head section <script type="text/javascript"> <!-- function goToURL() { //v3.0 var i, args=goToURL.arguments; document.returnValue = false; for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'"); } //--> </script> button <input name="button" type="submit" id="button" onclick="goToURL('parent','page.html');return document.returnValue" value="Submit" /> Quote Link to comment Share on other sites More sharing options...
OM2 Posted July 29, 2008 Author Share Posted July 29, 2008 thanks for the reply. wow... that looks complex! i've managed to find a solution: function goBack() { window.location = ('http://google.com'] ?>'); } and: <input type="button" value="Return" onclick="goBack()"> is ur answer better than what i've got above? let me know. thanks. Quote Link to comment Share on other sites More sharing options...
secoxxx Posted July 29, 2008 Share Posted July 29, 2008 not all browsers read that js well Quote Link to comment Share on other sites More sharing options...
OM2 Posted July 30, 2008 Author Share Posted July 30, 2008 so would u say ur answer is a better one? if it is: i'll use that instead. for what i'm using, it's not that much of a concern. the button is part of an admin system for the backend and it's upto the user to make sure they have a good enough browser with all updates to be able to use the admin. what he user sees does not have the button. BUT: i'd still rather have the more correct solution. let me know what u think. thanks. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted July 30, 2008 Share Posted July 30, 2008 head section <script type="text/javascript"> <!-- function goToURL() { //v3.0 var i, args=goToURL.arguments; document.returnValue = false; for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'"); } //--> </script> button <input name="button" type="submit" id="button" onclick="goToURL('parent','page.html');return document.returnValue" value="Submit" /> That's needlessly complex for a button to simply redirect the user. I mean eval()? A for-loop? Are you serious? A far easier way to do it: <script type="text/javascript"> window.onload = function() { var backButton = document.getElementById("backButton"); backButton.onclick = function() { location.href = "http://www.google.com/"; } } </script> . . . <button type="button" id="backButton" value="Go Back" /> Quote Link to comment Share on other sites More sharing options...
secoxxx Posted July 30, 2008 Share Posted July 30, 2008 I guess i like to do things the "hard" way. or i just like having the option of copy and pasting just the button and changing the page in the onclick of the button and your done. thats if i wanted to add another button. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted July 30, 2008 Share Posted July 30, 2008 I guess i like to do things the "hard" way. or i just like having the option of copy and pasting just the button and changing the page in the onclick of the button and your done. thats if i wanted to add another button. Yeah, but your function is still needlessly complex even for that. There's no benefit, at all, to using a for-loop or eval in your function. I mean, you can simply do something like: function goToUrl() { if(args.length == 2) { args[0] + ".location" = args[1]; } else { location.href = args[0]; //assuming args[0] is the location you want to go } } Same thing, executed in a much simpler way. In any event, I suggest you don't even do that. There's no reason for HTML to have knowledge of the script(s) working on it. So why place an onclick function call within a <button>? You don't gain any real flexablity. The only benefit is a few lines less of code to write. For me, I'd rather put everything in the <head>, like so: <script type="text/javascript"> window.onload = function() { var myButton = document.getElementById("myButton"); myButton.onclick = function() { location.href = "http://www.google.com/"; } } </script> Because then all my buttons are generic HTML elements, with only id's to determine their functionality. And if their locations are dynamically created, say, by a PHP script, I have one centralized place to put that code, instead of hunting through the HTML to find the right place to put it. Quote Link to comment Share on other sites More sharing options...
OM2 Posted July 30, 2008 Author Share Posted July 30, 2008 nightslyr i think ur ones looks good. i'll use that one. thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.