0

I have the following string

  "<iframe width="420" height="315" src="https://www.youtube.com/embed/DgPO56ImqUA?showinfo=1" frameborder="0" allowfullscreen></iframe>".

how can i get the value of src using javascript.

Vivacity InfoTech
  • 465
  • 3
  • 11
  • 25
  • possible duplicate of [substring/regex to get a src value held in a string](http://stackoverflow.com/questions/12393671/substring-regex-to-get-a-src-value-held-in-a-string) – Alex McMillan May 29 '15 at 08:38
  • @Ashok do you have a string or a HTML element can you please clarify – Vivek Gupta May 29 '15 at 08:46
  • 1
    before moving forward do check if your string is valid or not. It should be var srcString = ""; OR var srcString1 = ""; – Newinjava May 29 '15 at 08:49
  • If you have a `string` of HTML that contains `"` characters, you'll need to either escape them (eg: `" – Alex McMillan May 29 '15 at 08:52

2 Answers2

3

You can use a regular expression to pull the src from your string like so:

var myString = '<iframe width="420" height="315" src="https://www.youtube.com/embed/DgPO56ImqUA?showinfo=1" frameborder="0" allowfullscreen></iframe>';
var regex = /<iframe.*?src="(.*?)"/;

var src = regex.exec(myString)[1];

console.log(src);
Alex McMillan
  • 17,096
  • 12
  • 55
  • 88
0

Give a name to your frame such as

"name="frame1" width=..... />"

then use this alert(document.frames['frame1'].location.href);

Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34