替换' document.write()'

时间:2017-07-10 07:03:37

标签: javascript

我网站上的一些javascript使用' document.write()'

将用户的设备名称和位置放入网站文本中

最近它被许多浏览器屏蔽,代码没有被执行。

如何更换它以使其正常工作?我需要它同时或之前加载。

以下是我正在使用的两个脚本:



<script>
function x(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] || ''
    );
}
c = x('city')
m = x('model')
b = x('brand')
phone = x('brand') + ' ' + x('model')
browser = x('browser')
os = x('os')
ip = x('ip')
isp = x('isp')
osversion = x('osversion')
browserversion = x('browserversion')
</script>
  
<script type="text/javascript">
var dayNames = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var now = new Date();
var today = monthNames[now.getMonth()] + " " + now.getDate() + ", " + now.getFullYear();
function get_date(days)
	{return today = monthNames[now.getMonth()] + " " + (now.getDate() - days) + ", " + now.getFullYear();}
</script>
&#13;
&#13;
&#13;

这就是我试图实现它的方式:

&#13;
&#13;
<div class="main-content flag" id="content1">
<h1>Congratulations <script>document.write(b);</script> User! Your <script>document.write(m);</script> Awesome!</h1>
<div style="text-align:left; width:100%; color:black; font-size:10px;">
<script type="text/javascript">document.write(today);</script>
</div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

试试这个。

<div class="main-content flag" id="content1">
<h1>Congratulations <span id="brand-elem"></span> User! Your <span id="model-elem"></span> Awesome!</h1>
<div id="today-elem" style="text-align:left; width:100%; color:black; font-size:10px;">
</div>
</div>
<script type="text/javascript">
document.getElementById('brand-elem').innerHTML = b;
document.getElementById('model-elem').innerHTML = m;
document.getElementById('today-elem').innerHTML = today;
</script>

不要使用document.write。请参阅Why is document.write considered a "bad practice"?