Reputation: 15303
In my app, I have a field to fill by my user for their URL. After they fill I convert the url into a href link. But sometimes user does not add a http://
prefix to their link, so a link is not working properly..
Is there any way to open a link even without the http://
prefex in the href?
If anyone knows the way without using javascript is fine, else we can try with javascript.
Here is my
<a target="_blank" href="http://www.google.com">Google</a>
<br>
<a target="_blank" href="http://google.com">Google</a>
<br>
<a target="_blank" href="www.google.com">Google</a> // this is not work!
Upvotes: 1
Views: 2333
Reputation: 22817
You should force the user to insert a valid url, for instance by using a proper inpout
tag:
<input type="url" placeholder="Enter a valid URL - e.g. http://path.to/website">
In case you can't/you don't want to edit existing content
$('a[target="_blank"]').click(function(e){
e.preventDefault();
window.location = /^(http|https):/.test(this.href) ? this.href : 'http://' + this.href;
});
Upvotes: 1