简体   繁体   中英

Replacing 'document.write()'

Some javascript on my site places the user's device name as well as location into the text of the website using 'document.write()'

Recently it's been blocked by many browsers and the code isn't being executed.

How can I replace this to make it work correctly? I need it to load at the same time or before everything else.

Here are the two scripts I'm working with:

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

and this is how I'm attempting to implement it:

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

Try this one.

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

Don't use document.write. Refer Why is document.write considered a "bad practice"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM