开发者

Optimize javascript 1.2 for compatibility with prototype 1.6

开发者 https://www.devze.com 2023-01-14 05:29 出处:网络
I got this code, along with CMS when I try to add code using prototype 1.6 piece of code will be the first one and it will work

I got this code, along with CMS when I try to add code using prototype 1.6 piece of code will be the first one and it will work but second not work

this first piece

<script language="JavaScript1.2">
var slidemenu_width='120px'
var slidemenu_reveal='11px'
var slidemenu_top='78px'

var ns4=document.layers?1:0
var ie4=document.all
var ns6=document.getElementById&&!document.all?1:0

if (ie4||ns6)
document.write('<div id="slidemenubar2" style="left:'+((parseInt(slidemenu_width)-parseInt(slidemenu_reveal))*-1)+'px; top:'+slidemenu_top+'; width:'+slidemenu_width+'" onMouseover="pull()" onMouseout="draw()">')
else if (ns4){
document.write('<style>\n#slidemenubar{\nwidth:'+slidemenu_width+';}\n<\/style>\n')
document.write('<layer id="slidemenubar" left=0 top='+slidemenu_top+' width='+slidemenu_width+' onMouseover="pull()" onMouseout="draw()" visibility=hide>')
}

var sitems=new Array()

///////////Edit below/////////////////////////////////

//siteitems[x]=开发者_如何学Python["Item Text", "Optional URL associated with text"]

sitems[0]=["Tools", "cp.php?ac=magic&view=me"]
sitems[1]=["Presents", "gift.php?do=view"]


var target=""

/////////////////////////////////////////////////////////

if (ie4||ns4||ns6){
for (i=0;i<sitems.length;i++){
if (sitems[i][1])
document.write('<a href="'+sitems[i][1]+'" target="'+target+'">')
document.write(sitems[i][0])
if (sitems[i][1])
document.write('</a>')
document.write('<br/>\n')
}
}

function regenerate(){
window.location.reload()
}
function regenerate2(){
if (ns4){
document.slidemenubar.left=((parseInt(slidemenu_width)-parseInt(slidemenu_reveal))*-1)
document.slidemenubar.visibility="show"
setTimeout("window.onresize=regenerate",400)
}
}
window.onload=regenerate2

rightboundary=0
leftboundary=(parseInt(slidemenu_width)-parseInt(slidemenu_reveal))*-1

if (ie4||ns6){
document.write('</div>')
themenu=(ns6)? document.getElementById("slidemenubar2").style : document.all.slidemenubar2.style
}
else if (ns4){
document.write('</layer>')
themenu=document.layers.slidemenubar
}

function pull(){
if (window.drawit)
clearInterval(drawit)
pullit=setInterval("pullengine()",10)
}
function draw(){
clearInterval(pullit)
drawit=setInterval("drawengine()",10)
}
function pullengine(){
i    f ((ie4||ns6)&&parseInt(themenu.left)<rightboundary){
themenu.left=parseInt(themenu.left)+10+"px";
}
else if(ns4&&themenu.left<rightboundary)
themenu.left+=10
else if (window.pullit){
themenu.left=0
clearInterval(pullit)
}
}

function drawengine(){
if ((ie4||ns6)&&parseInt(themenu.left)>leftboundary){
themenu.left = parseInt(themenu.left)-10+"px";
}
//themenu.left=parseInt(themenu.left)-10+"px"
else if(ns4&&themenu.left>leftboundary)
themenu.left-=10
else if (window.drawit){
themenu.left=leftboundary
clearInterval(drawit)
}
}
</script>

second peace is my code it use prototype

I think the problem of conflict versions js ? How to alter the code to get rid javascript 1.2?


Try rewriting that code chunk, make a namespace to store variables, like this:

var tempNamespace={};
tempNamespace.slidemenu_width='120px'
tempNamespace.slidemenu_reveal='11px'
tempNamespace.slidemenu_top='78px'

tempNamespace.ns4=document.layers?1:0
tempNamespace.ie4=document.all
tempNamespace.ns6=document.getElementById&&!document.all?1:0

and change

<script language="JavaScript1.2">

into

<script type="text/javascript">

Also replace document.write code with constructing an html string and then putting it in a div where you want it to be.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号