views:

77

answers:

2

using jQuery i am trying to find out all the URLS that user has entered which are not starting with http or https and finally i want to prepend http to all such URLs so that when user clicks on them they are taken to a proper site instead of broken link caused due to entry of URLs without http or https.

Also like to mention that User have a field "Websites they Like" where they enter websites of their interest. So if they like stackoverflow, they may end up writing www.stackoverflow.com which will be considered a relative link without http.

Also my requirments are such that i cant prompt user to enter http or https before there urls

+2  A: 

If you're looking to "patch" the URLs of already-rendered anchor tags to include http://, you could probably run through them with something like this:

$(document).ready( function(){
    $('a:not([href^="http://"]):not([href^="https://"])').each( function(){
        $(this).attr('href', 'http://' + $(this).attr('href'));
    })
})

Edit: revised to use each(). Edit: revised to handle http and https.

Ken Redler
this is what i have written exactly; it didnt worked for me, did u tried
sushil bharwani
just forget about https and http. imagine that its just http links still the line of code you have written does not works.
sushil bharwani
The above code, after the edits, works on a quick test page I threw together. My test includes a link with no protocol, one with http:// in place, and one with https:// in place. If it doesn't work for you, can you post more of your page?
Ken Redler
ken please look into my answer below. Actually $(this) is not working for me. can u suggest why
sushil bharwani
It's the scope of $(this), which would be "document". I made the same mistake in my initial ramblings. Changing to an each() approach means $(this) inside the each() function refers to the individual selected anchor tag.
Ken Redler
Good help; it would be great if you can tell me something where i can understand about the scope of div. It still not clear
sushil bharwani
A: 

I got to know what i was doing wrong i also used


$(document).ready( function(){ 
    $('a:not([href^="http://"])').attr('href', 'http://' + $(this).attr('href')); 
}) 

so the selector was working fine but 'http://' + $(this).attr('href') was not working. when i replaced $(this) with $('a:not([href^="http://"])') it worked. But now i am confused why $(this) wont work

sushil bharwani
Yes, this is why I switched to using each() -- the context of $(this) is "document", not the selector. The approach above may cause problems, since you're building a new set of selected objects instead of focusing on the selected anchor tag.
Ken Redler
can you please elaborate on context of this is document. I didnt followed it. How would i be knowing abt the context.
sushil bharwani
i also didnt followed; "since you're building a new set of selected objects instead of focusing on the selected anchor tag." please explain
sushil bharwani
The first selector identifies the set of anchor tags without http://. You then do ANOTHER new, fresh selection in the attr() portion -- selecting that set of tags again. This second selection is unrelated to your first selection. If it does work, it may be coincidental. Try your code on a page with many links, and I think you'll see the problem. In any case, the code I posted above seems to work properly given a test page with a half-dozen links with and without http:// and https://.
Ken Redler
Yes the code you posted works perfect for me; but i am still doubtful bcoz of your comment if i should be using it or not.What happens if the number of links grows up
sushil bharwani
I think you'll be fine for any reasonable page size. But if you told me you have 2000 links to "fix," I'd consider something on the server side. Working on the server side, you'll also have a solution that works for people with no javascript.
Ken Redler