简体   繁体   English

使用inner.html

[英]use of inner.html

Apologies for the strange format of this question. 对这个问题的奇怪格式表示歉意。 I've asked a couple of similar questions recently and am still at a loss. 我最近问了几个类似的问题,但仍然不知所措。 I REALLY don't know how most of this works, and I've been trying to follow various tutorials etc. on places like W3Schools.com, and with the help of people here, and although I don't fully understand, I think I'm almost there. 我真的不知道大部分是如何工作的,我一直在尝试在W3Schools.com这样的地方学习各种教程,并且在这里的人的帮助下,虽然我不完全理解,但我认为我快到了。

What I'm trying to do is ONLY play a piece of video if it's the first time a visitor has arrived at the site (but it's a bit special - it's transparent, and needs to float on the page). 我想要做的只是播放一段视频,如果这是第一次访问者到达网站(但它有点特别 - 它是透明的,需要漂浮在页面上)。

The code below correctly sets a cookie if one doesn't exist, and checks for it on the user visiting the site. 如果一个cookie不存在,下面的代码会正确设置一个cookie,并在访问该站点的用户上检查它。 The bit I have a problem with is the syntax of the line that starts: 我遇到问题的是该行的语法:

document.getElementById('video').innerHTML =

If I comment that line out, all the other cookie stuff works fine, so I'm hoping that someone can help. 如果我评论该行,所有其他cookie的东西工作正常,所以我希望有人可以提供帮助。 the errors that the firefox error console gives are: unexpected end of xml source and checkCookie is not defined firefox错误控制台给出的错误是:xml源的意外结束和checkCookie未定义

I also tried throwing quotes around the whole lot, but it made no real difference. 我也试过在整个地方投掷报价,但没有真正的区别。

Here is the code, and thank you all in advance. 这是代码,并提前感谢大家。 Rob. 抢。

<div id="video" style="position:absolute;top:125px;left:-67px;z-index:10000000"> </div>
<script src="AC_RunActiveContent.js" type="text/javascript"> </script>

<script type="text/javascript">
function getCookie(c_name)
{
  var i,x,y,ARRcookies=document.cookie.split(";");

  for (i=0;i<ARRcookies.length;i++)
  {
    x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y = ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x = x.replace(/^\s+|\s+$/g,"");

    if (x==c_name)
    {
      return unescape(y);
    }
  }
}

function setCookie(c_name,value,exdays)
{
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + exdays);
  var c_value = escape(value) + ((exdays == null) ? "" : "; expires="+exdate.toUTCString());
  document.cookie=c_name + "=" + c_value;
}

function checkCookie()
{
  var username=getCookie("username");

  if (username!=null && username!="")
  {
    alert("Welcome again " + username);
  }
  else
  {
    username=prompt("Please enter your name:","");

    if (username!=null && username!="")
    {
      setCookie("username",username,7);
      document.getElementById('video').innerHTML = <script type="text/javascript"> AC_FL_RunContent ('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width','320','height','220','id','HTIFLVPlayer','src','HTIFLVPlayer','flashvars','&MM_ComponentVersion=1&skinName=HTI_Skin&streamName=nigel&autoPlay=true&autoRewind=true','quality','high','scale','noscale','name','HTIFLVPlayer','salign','lt','pluginspage','http://www.macromedia.com/go/getflashplayer','wmode','transparent','movie','HTIFLVPlayer')</script>; 
    }
  }
}
</script>
<body onload="checkCookie()">
</body>

Aren't you missing a few quotation marks there? 你不是错过了几个引号吗?

document.getElementById('video').innerHTML = <script type="text/javascript"> AC_FL_RunContent ('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width','320','height','220','id','HTIFLVPlayer','src','HTIFLVPlayer','flashvars','&MM_ComponentVersion=1&skinName=HTI_Skin&streamName=nigel&autoPlay=true&autoRewind=true','quality','high','scale','noscale','name','HTIFLVPlayer','salign','lt','pluginspage','http://www.macromedia.com/go/getflashplayer','wmode','transparent','movie','HTIFLVPlayer')</script>; 

vs

document.getElementById('video').innerHTML = "<script type='text/javascript'> AC_FL_RunContent ('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width','320','height','220','id','HTIFLVPlayer','src','HTIFLVPlayer','flashvars','&MM_ComponentVersion=1&skinName=HTI_Skin&streamName=nigel&autoPlay=true&autoRewind=true','quality','high','scale','noscale','name','HTIFLVPlayer','salign','lt','pluginspage','http://www.macromedia.com/go/getflashplayer','wmode','transparent','movie','HTIFLVPlayer')</script>"; 

A couple of things that may or may not lead you the answer: 一些可能会或可能不会引导您答案的事情:

Check that the returned value from getElementById is not null before trying to use it, eg: 在尝试使用之前检查getElementById的返回值是否为null,例如:

var el = document.getElementById('video')
if (el) {
  el.innerHTML = ...
}

Setting a script element as the innerHTML of an element will not execute the script. 将script元素设置为元素的innerHTML将不会执行脚本。

The best way to do that is to put the script into a separate file and load it using the src property of the script element, eg 最好的方法是将脚本放入一个单独的文件中,并使用script元素的src属性加载它,例如

var script = document.createElement('script');
script.src = 'videoScript.js';  // or whatever you call the script file
el.appendChild(script);

Alternatively, you can set the script as the script element's .text property, but that will not work in some browsers: 或者,您可以将脚本设置为脚本元素的.text属性,但这在某些浏览器中不起作用:

script.text = '...';
el.appendChild(script);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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