views:

30617

answers:

5

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'));"&gt;click here</a>``
+3  A: 
<a href="javascript:;" onclick="window.location = 'http://example.com/submit.php?url=' + escape(document.location.href);'">Go</a>;
Stephen
+5  A: 

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'));"&gt;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?

keparo
+1  A: 

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="&gt;...&lt;/a&gt;

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?

nickf
A: 

Good question - the reason I'm not doing it server side is that I'm trying to write a bookmarklet.

55skidoo
A: 

How to i close current browser and open new by cliking a link once.