55skidoo
55skidoo

Reputation: 769

Javascript: open new page in same window

Is there an easy way to modify this code so that the target URL opens in the SAME window?

<a href="javascript:q=(document.location.href);void(open('http://example.com/submit.php?url='+escape(q),'','resizable,location,menubar,toolbar,scrollbars,status'));">click here</a>``

Upvotes: 59

Views: 431685

Answers (8)

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92347

try

<a href="#" 
   onclick="location='http://example.com/submit.php?url='+escape(location)"
   >click here</a>

Upvotes: 0

Felceris Juozas
Felceris Juozas

Reputation: 31

Here's what worked for me:

<button name="redirect" onClick="redirect()">button name</button>
<script type="text/javascript">
function redirect(){
var url = "http://www.google.com";
window.open(url, '_top');
}
</script>

Upvotes: 3

Brett Melton
Brett Melton

Reputation: 11

So by adding the URL at the the end of the href, Each link will open in the same window? You could also probably use _BLANK within the HTML to do the same thing.

Upvotes: 1

parwaze
parwaze

Reputation: 821

<script type="text/javascript">
window.open ('YourNewPage.htm','_self',false)
</script>

see reference: http://www.w3schools.com/jsref/met_win_open.asp

Upvotes: 82

Valentine Nzekwe
Valentine Nzekwe

Reputation: 31

try this it worked for me in ie 7 and ie 8

 $(this).click(function (j) {
            var href = ($(this).attr('href'));
            window.location = href;
            return true;

Upvotes: 3

keparo
keparo

Reputation: 34129

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

Set it to: "_self".

<a href="javascript:q=(document.location.href);void(open('http://example.com/submit.php?url='+escape(q),'_self','resizable,location,menubar,toolbar,scrollbars,status'));">click here</a>


Sidenote: The following question gives an overview of an arguably better way to bind event handlers to HTML links.

What's the best way to replace links with js functions?

Upvotes: 74

nickf
nickf

Reputation: 545985

I'd take that a slightly different way if I were you. Change the text link when the page loads, not on the click. I'll give the example in jQuery, but it could easily be done in vanilla javascript (though, jQuery is nicer)

$(function() {
    $('a[href$="url="]')    // all links whose href ends in "url="
        .each(function(i, el) {
            this.href += escape(document.location.href);
        })
    ;
});

and write your HTML like this:

<a href="http://example.com/submit.php?url=">...</a>

the benefits of this are that people can see what they're clicking on (the href is already set), and it removes the javascript from your HTML.

All this said, it looks like you're using PHP... why not add it in server-side?

Upvotes: 1

Stephen Walcher
Stephen Walcher

Reputation: 2575

<a href="javascript:;" onclick="window.location = 'http://example.com/submit.php?url=' + escape(document.location.href);'">Go</a>;

Upvotes: 7

Related Questions