Belmark Caday
Belmark Caday

Reputation: 1683

Why is window.open prepending a default url?

Code

<a href="www.someurl.com" id="equipment_url_readonly">www.someurl.com</a>

$(document).on('click','#equipment_url_readonly', function(event) {
    event.preventDefault();
    var url = $(this).attr('href');
    console.log(url); //It console logs correctly the url
    window.open(url,"_blank");
});

But whenever I click on the anchor tag, it prepends the url of the parent,
so for example my current page that has that url is www.mycurrentpage.com/this-page. When I click on that link, it opens a new window with a url of:

www.mycurrentpage.com/this-page/www.someurl.com

Any idea how this happened?

Upvotes: 2

Views: 4049

Answers (3)

Govind Singh
Govind Singh

Reputation: 15490

try the below, i think its enough for your purpose. hope it will helps.

you have to use http:// or // before your url

<a href="javascript:void(0);" onclick="window.open('http://www.someurl.com', '_blank');" >www.someurl.com</a>

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173572

As mentioned in the comments, the problem is that your anchor doesn't contain an absolute URL because it misses the protocol (i.e. "http://" or even "//").

This should fix it:

<a href="//www.someurl.com" id="equipment_url_readonly">www.someurl.com</a>

The above will work for both secure and non-secure pages.

Upvotes: 3

Bemmu
Bemmu

Reputation: 18217

You can accomplish opening a new window without any Javascript.

<a href="http://www.someurl.com" target=_blank>www.someurl.com</a>

Upvotes: 1

Related Questions