简体   繁体   中英

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.

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. 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. the errors that the firefox error console gives are: unexpected end of xml source and checkCookie is not defined

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:

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

Setting a script element as the innerHTML of an element will not execute the script.

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

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:

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

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