簡體   English   中英

使用inner.html

[英]use of inner.html

對這個問題的奇怪格式表示歉意。 我最近問了幾個類似的問題,但仍然不知所措。 我真的不知道大部分是如何工作的,我一直在嘗試在W3Schools.com這樣的地方學習各種教程,並且在這里的人的幫助下,雖然我不完全理解,但我認為我快到了。

我想要做的只是播放一段視頻,如果這是第一次訪問者到達網站(但它有點特別 - 它是透明的,需要漂浮在頁面上)。

如果一個cookie不存在,下面的代碼會正確設置一個cookie,並在訪問該站點的用戶上檢查它。 我遇到問題的是該行的語法:

document.getElementById('video').innerHTML =

如果我評論該行,所有其他cookie的東西工作正常,所以我希望有人可以提供幫助。 firefox錯誤控制台給出的錯誤是:xml源的意外結束和checkCookie未定義

我也試過在整個地方投擲報價,但沒有真正的區別。

這是代碼,並提前感謝大家。 搶。

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

你不是錯過了幾個引號嗎?

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

一些可能會或可能不會引導您答案的事情:

在嘗試使用之前檢查getElementById的返回值是否為null,例如:

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

將script元素設置為元素的innerHTML將不會執行腳本。

最好的方法是將腳本放入一個單獨的文件中,並使用script元素的src屬性加載它,例如

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

或者,您可以將腳本設置為腳本元素的.text屬性,但這在某些瀏覽器中不起作用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM