如何在 iframe src 中使用 javascript 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8642488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use javascript variable in iframe src
提问by Johnny
how to write javascript variable in iframe src?
如何在 iframe src 中编写 javascript 变量?
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?
这里 ReturnURL() 是一个返回值的 javascript 函数。但问题是在 iframe 源中我没有得到函数的返回值。我是不是没有输入正确的格式或遗漏了什么?
Thanks in advance Johnny
提前致谢 约翰尼
回答by nnnnnn
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:
您不能以这种方式直接在 html 标记中使用 JavaScript 变量或函数。你可以做的是先定义你的 iframe,然后从 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.
还有其他几种方法可以实现类似的目标,但是当我不确定您的上下文是什么时,我真的不想把它们全部都讲一遍。
回答by Umesh Patil
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>');