admin管理员组

文章数量:1332389

I am using this to open on a new window:

<div class="backbutton" OnClick="javascript: window.open('')">Go back</div>

How to amend this if I want onclick to open a hyperlink on the same page?

thanks.

I am using this to open on a new window:

<div class="backbutton" OnClick="javascript: window.open('http://www.goesback.')">Go back</div>

How to amend this if I want onclick to open a hyperlink on the same page?

thanks.

Share asked Jan 12, 2012 at 16:59 user1065673user1065673 931 gold badge1 silver badge5 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 1

You just set the url to the window.location.href

so

<div class="backbutton" OnClick="window.location.href = 'http://www.goesback.'">Go back</div>

But you should really just convert it to a regular link

<a class="backbutton" href="http://www.goesback.">Go back</div>

location.href='http://www.goesback.'

Though you might want to reconsider using onclick to trigger that.

Why not just use a simple anchor tag?

<a href="[where you want to go]">Go There</a>

If you need to use JavaScript, the other answers - setting the window.location property - will do what you want.

The second parameter of window.open() is a string representing the name of the target window.

Set it to: "_self".

<div class="backbutton" OnClick="javascript: window.open('http://www.goesback.','_self')">Go back</div>

Set the window.location.href property:

<div class="backbutton" OnClick="window.location.href='http://www.goesback.'">
    Go back
</div>

Or just use a plain old anchor (probably your best option):

<a href="http://www.goesback.">Go back</a>

Note: the javascript: prefix is not needed in the onclick handler.

<div class="backbutton" OnClick="window.location.href = 'http://www.goesback.'">Go back</div>

本文标签: How to open a hyperlink on the same page with javascriptStack Overflow