在script标记的src中使用document.getElementById
Use document.getElementById in the src of script tag
我想在script标记的src中使用document.getElementById
像这样:
<div id="testTracking">
<script type="text/JavaScript" language="JavaScript" src="https://test.com/tracking/hio_wm.js?state="+document.getElementById("state").value+ "&zip=" +document.getElementById("zipcode").value+ ""></script>
</div>
是否可以在Script标签的SRC属性中使用document.getElementById
标签。
如果没有,那么请建议如何实现。
感谢
<script type="text/javascript">
document.write('<script type="text/javascript" src="https://test.com/tracking/hio_wm.js?state='+document.getElementById("state").value+ '&zip=' +document.getElementById("zipcode").value + '"></script>');
</script>
您可能需要动态创建它:
var ss = document.createElement('script');
ss.src = "https://test.com/tracking/hio_wm.js?state="
+document.getElementById("state").value+ "&zip="
+document.getElementById("zipcode").value;
好的,你可以用document.write来完成,但你必须确保DOM已经完全加载。您可以使用jQuery。
<script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
<script type="text/javascript">
$().ready(function()
{
document.write('<script type="text/javascript" src="https://test.com/tracking/hio_wm.js?state='+document.getElementById("state").value+ '&zip=' +document.getElementById("zipcode").value + '></script>');
});
</script>
我想,您将值放在服务器端的state
和zipcode
元素中。例如,在asp.net中:
<input type="text" value="<%: Model.State %>" id="state" />
<input type="text" value="<%: Model.Zip %>" id="zipcode" />
如果是这样,你可以这样做:
<script type="text/JavaScript" language="JavaScript" src="https://test.com/tracking/hio_wm.js?state=<%: Model.State%>&zip=<%:Model.Zip%>"></script>
<div id="testTracking">
<script type="text/JavaScript" language="JavaScript" src="https://test.com/tracking/hio_wm.js"></script>
<script type="text/javascript">
var state = document.getElementById("state").value;
var zip = document.getElementById("zipcode").value;
initializeData(state, zip)
</script>
</div>
和在hio_wm.js:
function initializeData( state, zipcode )
{
//do whatever you need with that
}
相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 如何用更合适的内容替换document.write
- document.styleSheets不返回任何内容
- jQuery document.ready not working
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- reactjs this.refs vs document.getElementById
- 如何通过adf中的document.getElementById获取inputText字段值
- window.opener.document在ie中不起作用
- contentWindow.document.body is null
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- document.getElementById(“st”).click();不起作用
- $(document).height和$(window).heaght都返回相同的值-使用的是正确的doctype
- jQuery document.ready停止代码
- document.getElementById并使用id名称
- 使用jQuery 1.8.1准备好多个$(document).是否有任何开销
- document.getElementById在js中不起作用
- document.getElementById.style.backgroundImage not working
- 停止对document.ready函数的重定向/刷新
- 是否可以 document.getElementsByTagName('head')[0] ever 返回 null