2

I have a string value that holds a hyperlink. I want only the value after url which is: http://start.somesite.net/media/videos/flash/12345.flv . The value after url= is dynamic so how i can grab that dynamic value and put it as variable?

this is the input string value:

 <h4>
        <a href="http://www.somesite.com/season/?url=http://start.somesite.net/media/videos/flash/12345.flv"
    target="_blank">Download Episode 3</a>
2
  • Wow, surprised how many people are treating this as a DOM element when it says "this is the input string value:" Commented Dec 7, 2012 at 22:24
  • so the input string is broken html? (h4 not closed in the example) Commented Dec 7, 2012 at 23:17

6 Answers 6

3

Try:

var url = href.match(/url=(.+)/)[1]
Sign up to request clarification or add additional context in comments.

2 Comments

url= doesn't exist in the input string
where does it say url=? <h4> <a href="http://www.somesite.com/season/?url=http://start.somesite.net/media/videos/flash/12345.flv" target="_blank">Download Episode 3</a>
1

If you are using jquery try this:

   var link=$('#id_of_the_tag_a').attr('href');
   var linkSplitted=link.split('?url=');
   var final=linkSplitted[1];

If u want you can go here to see the result

1 Comment

The index in jsFiddle is [1]. Just sayin'
0

var url = input_str.match(/url=([^"]+)"/)[1];

3 Comments

Actually, he's looking for a part after url= not href=.
It happens to all of us sometimes. (I wasn't the one who down voted you btw)
Thanks to brian cray and the rest.brian cray your solution gave me the flv url i wanted!
0
document.getElementsByTagName("a")[0].href

Comments

0

Turning the string into an actual anchor makes working with url strings a breeze

var div = ​docu​ment.createElement('div');
div.innerHTML = '<a href="http://www.somesite.com/season/?url=http://start.somesite.net/media/videos/flash/12345.flv" target="_blank">Download Episode 3</a>'​​​​​​​​​​​

var a = div.querySelectorAll('a');
alert(a[0].search.split('=')[1]);

​ I like this method because it allows you to get all kinds of information if you ever need it:

a[0].protocol
a[0].hostname
a[0].hash
a[0].port
a[0].innerText

1 Comment

+1 This would be an elegant solution if the input string was regular html (not broken as in the example). And split('?url=') might work even better.
0

Step by step: Give your a tag one id.

var yourTag = document.getElementsById("your_tag_id"); //Get ref to your tag
var str = yourTag.getAttribute("href"); //Get the entire url
var arr = str.split("="); //Break in two parts.
str = arr[1]; //Take the second part.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.