Jump to content

Recommended Posts

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

 

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.

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" />

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.

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.

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" />

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.