在 script 标签的 src 中使用 document.getElementById

发布于 2024-11-25 04:08:19 字数 464 浏览 2 评论 0原文

我想在 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 标签。

如果否,请建议如何可能。

谢谢

I want to use document.getElementById in the src of script tag
like this:

<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>

Can it is possible to use document.getElementById tags within SRC attribute of Script tag .

If No then plz suggest how it can be possible .

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苏璃陌 2024-12-02 04:08:19
<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>
<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>
把时间冻结 2024-12-02 04:08:19

您可能想动态创建它:

var ss = document.createElement('script');
ss.src = "https://test.com/tracking/hio_wm.js?state="
    +document.getElementById("state").value+ "&zip="    
    +document.getElementById("zipcode").value;

You may want to create it dynamically :

var ss = document.createElement('script');
ss.src = "https://test.com/tracking/hio_wm.js?state="
    +document.getElementById("state").value+ "&zip="    
    +document.getElementById("zipcode").value;
秋叶绚丽 2024-12-02 04:08:19

好的,你可以使用 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>

ok, you can do it with document.write but you have to make sure that the DOM is already fully loaded. You can use jQuery for that.

<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>
×纯※雪 2024-12-02 04:08:19

我想,您将值放入服务器端的 statezipcode 元素中。例如,在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>

I suppose, you put values in your state and zipcode elements on the server side. E.g., in asp.net:

<input type="text" value="<%: Model.State %>" id="state" />
<input type="text" value="<%: Model.Zip %>" id="zipcode" />

If so, you can do it this way:

<script type="text/JavaScript" language="JavaScript" src="https://test.com/tracking/hio_wm.js?state=<%: Model.State%>&zip=<%:Model.Zip%>"></script>
つ可否回来 2024-12-02 04:08:19
<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
}
<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>

And the in hio_wm.js:

function initializeData( state, zipcode )
{
    //do whatever you need with that
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文