I want 开发者_StackOverflow社区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
<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>
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;
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>
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>
<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
}
精彩评论