How to use javascript variable in iframe src

…衆ロ難τιáo~ 提交于 2019-12-08 23:37:47

问题


how to write javascript variable in iframe src?

Like

<iframe id="showskill" scrolling="yes" height="350" width ="350" src="http://localhost/POSkill/skillshow.aspx?user_id="+ ReturnURL() ></iframe>

Here ReturnURL() is a javascript function which returns a value. But the problem is in the iframe source I'm not getting the returned value of the function. Am I not putting in the right format or missing something?

Thanks in advance Johnny


回答1:


You can't use JavaScript variables or functions directly within your html markup in that manner. What you can do is define your iframe first and then set its source from JavaScript:

<iframe id="showskill" scrolling="yes" height="350" width ="350" src=""></iframe>

<script>
    document.getElementById("showskill").src =
              "http://localhost/POSkill/skillshow.aspx?user_id="+ ReturnURL();
</script>

There are several other ways to achieve something similar, but I don't really want to go through them all when I'm not sure quite what your context is.




回答2:


You can not append the variable returned by function direcly as you did here. Do, something as given below.

var url=ReturnURL();
var urlPath='http://localhost/POSkill/skillshow.aspx?user_id='+url;
document.write('<iframe id="showskill" scrolling="yes" height="350" width ="350" src="'+urlPath+'><\/iframe>');


来源:https://stackoverflow.com/questions/8642488/how-to-use-javascript-variable-in-iframe-src

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!