简体   繁体   中英

How do I add html attribute value into a javascript variable

Hi I am working on a project that is based on asp and javascript. Also I am new to both programming languages.

I created a popup window that displays a video. When you hit its "close" button, the popup would close but the audio kept playing in the background.

With this code now my audio stops. But it leads to another issue.

So here is the code.

<iframe id="pict" width="560" height="315" src="somelink" frameborder="0"   allowfullscreen></iframe>

I created a javascript variable - to copy the 'iframe src' value to it.

<script type="javascript"> 
var addurl = document.getElementById('#pict').src;
</script>

Then for 'onclick' in my span, I empty the src (so that the audio stops) and re-add the above variable value back in the src.(so that the user can play the video again in the same session)

onclick= ""$('#pict').attr('src','');
$('#pict').attr('src','"& addurl & "');"" > 

I dont think the 'addurl' variable is working correctly here. AS I can't play the video twice in the same session. Its blank the second time.

How can I add iframe (src) value inside a variable?? I would appreciate if I could get any help to solve this problem.

As mentioned in my comment above, you have an extra > that closes your <iframe> before you set you id, width, height and other attributes. You simply need to remove the extra > and all should be fine.

Simple Demo

<iframe> id="pict" width="560" height="315" src="somelink" frameborder="0"   allowfullscreen></iframe>
       ^ - Remove this
<iframe id="pict" width="560" height="315" src="somelink" frameborder="0"   allowfullscreen></iframe>

It looks like you also have some extra quotes "" inside of your javascript and miscellaneous & s.

onclick= ""$('#pict').attr('src','');
         ^^ - Remove the quotes
onclick= $('#pict').attr('src','');

$('#pict').attr('src','"& addurl & "');"" >
                        ^ ------ ^ --- ^^ - Remove the `'`s `&`s and quotes
$('#pict').attr('src',addurl);>

Well, it seems a bit confusing. You have some options here, so I give you a few pointers on how to understand the code better.

If you are working inside the page content, you can insert ASP code like this:

<p>lorem ipsum: "<% =my_value %>" is my value</p>

In your code, it looks, as if you are inside a script block, building a string:

<%
  dim some_asp_variable
  dim my_value

  some_asp_variable = "<p>Lorem ipsum: """ & my_value & """ is my value</p>"

  Response.write some_asp_variable
%>

Note, how you have to write double quotes to put one quote in the string. and use & to concatenate the strings.

The part you have given:

 onclick= ""$('#pict').attr('src','');$('#pict').attr('src','"& addurl & "');"" > 

is correct, but only if you are composing a string in ASP (the second option).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM