最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - replacing whole HTMLinside body tag - Stack Overflow

programmeradmin1浏览0评论

I want to replace the whole HTML inside body tag with some content and I use these codes inside both head and body sections, but in the fist code it doesn't show the content and in the second code it shows the content but doesn't clear ex-content.

the first code:

<script>

var content = 'some content';

if(navigator.appName=="Netscape")
{
document.getElementsByTagName('body').innerHTML = content; 
}

</script>

the second code:

<script>

var content = 'some content';

if(navigator.appName=="Netscape")
{
document.body.innerHTML = content; 
}

</script>

also I use some php code in the document

what should I do?
thanks

I want to replace the whole HTML inside body tag with some content and I use these codes inside both head and body sections, but in the fist code it doesn't show the content and in the second code it shows the content but doesn't clear ex-content.

the first code:

<script>

var content = 'some content';

if(navigator.appName=="Netscape")
{
document.getElementsByTagName('body').innerHTML = content; 
}

</script>

the second code:

<script>

var content = 'some content';

if(navigator.appName=="Netscape")
{
document.body.innerHTML = content; 
}

</script>

also I use some php code in the document

what should I do?
thanks

Share Improve this question asked Oct 22, 2012 at 12:45 user1749030user1749030 1711 gold badge3 silver badges12 bronze badges 7
  • 4 Is this a joke? Are you writing code for extinct browsers, or what? – Jukka K. Korpela Commented Oct 22, 2012 at 12:50
  • 2 Looks like some W3Schools example code to me :) – Laurence Commented Oct 22, 2012 at 12:51
  • Don't use browser detection at all; try to use feature detection instead. – feeela Commented Oct 22, 2012 at 12:52
  • what is the problem with the above code? – JSK NS Commented Oct 22, 2012 at 13:08
  • honestly, you shouldn't do anything, I don't think Netscape is used anymore. – BBog Commented Oct 22, 2012 at 13:44
 |  Show 2 more comments

3 Answers 3

Reset to default 14

.getElementsByTagName returns an array so you have to use [0] to get the first element:

<script type="text/javascript">

    var content = 'some content';
    document.getElementsByTagName('body')[0].innerHTML = content; 

</script>

Here is an example: http://jsfiddle.net/MWjsc/

I believe that you want to display some special content like browser not supported for netscape.

You can use document.write document.write(content); this will replace total document content.

Get fancy:

document.documentElement.innerHTML = document.implementation.createHTMLDocument().documentElement.outerHTML
// <html><head></head><body></body></html>
发布评论

评论列表(0)

  1. 暂无评论