Javascript: open new page in same window

JavascriptWindow

Javascript Problem Overview


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>``

Javascript Solutions


Solution 1 - Javascript

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

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

Solution 2 - Javascript

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?

Solution 3 - Javascript

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

Solution 4 - Javascript

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;

Solution 5 - Javascript

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>

Solution 6 - Javascript

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?

Solution 7 - Javascript

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.

Solution 8 - Javascript

try

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question55skidooView Question on Stackoverflow
Solution 1 - JavascriptparwazeView Answer on Stackoverflow
Solution 2 - JavascriptkeparoView Answer on Stackoverflow
Solution 3 - JavascriptStephen WalcherView Answer on Stackoverflow
Solution 4 - JavascriptValentine NzekweView Answer on Stackoverflow
Solution 5 - JavascriptFelceris JuozasView Answer on Stackoverflow
Solution 6 - JavascriptnickfView Answer on Stackoverflow
Solution 7 - JavascriptBrett MeltonView Answer on Stackoverflow
Solution 8 - JavascriptKamil KiełczewskiView Answer on Stackoverflow