Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12
  1. #1
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    costrutto JavaScript poco chiaro

    Buona sera, ho visto un applet JAVA sul sito HTML.it (http://www.html.it/java/a451/applet.htm) e ho riprodotto lo script
    in JavaScript. Pero quando ho riprodotto lo script ho fatto riferimento ad una costrutto visto in un altro script (JavaScript) che si trova sempre su HTML.it (http://www.html.it/mailing/archivio/a125/esempio.htm) però di questo script non ho mai capito una parte, potete spiegarmi che significa? ve la delimiterò con dei commenti (/* questa è la parte che non capisco */) nel HTML del codice:

    codice:
    <HTML>
    
    <HEAD>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <TITLE>HTML.it - il sito italiano sul Web publishing | Un JavaScript a settimana | HTML.it</TITLE>
    
    <style>
    A:link, A:visited { text-decoration: none }
    A:hover { text-decoration: underline }
    </style>
    <style>
    
    #divMenu {font-family:arial,helvetica; font-size:8pt; font-weight:bold} //grandezza testo
    #divMenu a{text-decoration:none;}
    #divMenu a:hover{color: black;}
    </style>
    <script language="JavaScript1.2">
    
    ie=document.all?1:0
    n=document.layers?1:0
    
    
    lshow=40
    
    
    var move=30;
    
    
    menuSpeed=40
    
    
    var moveOnScroll=true
    
    
    var tim;
    var ltop;
    
    function makeMenu(obj,nest){
        nest=(!nest) ? '':'document.'+nest+'.'
        this.css=(n) ? eval(nest+'document.'+obj):eval(obj+'.style')
    	this.state=1
    	this.go=0
    	this.width=n?this.css.document.width:eval(obj+'.offsetWidth')
    	this.left=b_getleft
    
    /* questa è la parte che non capisco */
    
        this.obj = obj + "Object"; 	eval(this.obj + "=this") // questa è la parte che non capisco
    
    /* questa è la parte che non capisco */
    
    }
    
    function b_getleft(){
    	var gleft=(n) ? eval(this.css.left):eval(this.css.pixelLeft);
    	return gleft;
    }
    
    function moveMenu(){
    	if(!oMenu.state){
    		clearTimeout(tim)
    		mIn()
    	}else{
    		clearTimeout(tim)
    		mOut()
    	}
    }
    
    function mIn(){
    	if(oMenu.left()>-oMenu.width+lshow){
    		oMenu.go=1
    		oMenu.css.left=oMenu.left()-move
    		tim=setTimeout("mIn()",menuSpeed)
    	}else{
    		oMenu.go=0
    		oMenu.state=1
    	}
    }
    
    function mOut(){
    	if(oMenu.left()<0){
    		oMenu.go=1
    		oMenu.css.left=oMenu.left()+move
    		tim=setTimeout("mOut()",menuSpeed)
    	}else{
    		oMenu.go=0
    		oMenu.state=0
    	}
    }
    
    function checkScrolled(){
    	if(!oMenu.go) oMenu.css.top=eval(scrolled)+ltop
    	if(n) setTimeout('checkScrolled()',30)
    }
    
    function menuInit(){
    	oMenu=new makeMenu('divMenu')
    	scrolled=n?"window.pageYOffset":"document.body.scrollTop"
    	oMenu.css.left=-oMenu.width+lshow
    	ltop=(n)?oMenu.css.top:oMenu.css.pixelTop;
    	oMenu.css.visibility='visible'
    	if(moveOnScroll) ie?window.onscroll=checkScrolled:checkScrolled();
    }
    
    
    onload=menuInit;
    </script>
    
    </HEAD>
    <BODY  BGCOLOR="white" vlink="blue" link="blue">
    
    <div id="divMenu" style="position:absolute; top:20; left:120; width:200; visibility:hidden; background-color:F0F0F0">
    <nobr>
    	Html.it -
    	Forum -
    	Flash-mx.it -
    	MENU
    </nobr>
    </div>
    
    
    
    
    
    <div align="center"><font color="#333333">clicca sul link MENU</font></div>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    </div>
    
    <font size=1 face="arial">
    <div align="center">
    <SCRIPT LANGUAGE="JAVASCRIPT">
    <!--
    var browName = navigator.appName;
    var browDateTime = (new Date()).getTime();
    var browVersion = parseInt(navigator.appVersion);
    var ua=navigator.userAgent.toLowerCase();
    var adcode='';
    if (browName=='Netscape'){
         if (browVersion>=5)
              { document.write('<ifr'+'ame src="http://ad.html.it/banman.asp?ZoneID=36&Task=Get&Browser=NETSCAPE6&X=' + browDateTime + '" width=468 height=60 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No></ifr'+'ame>'); }
         else if ((browVersion>=4)&&(ua.indexOf("mac")==-1))
              { document.write('<S'+'CRIPT src="http://ad.html.it/banman.asp?ZoneID=36&Task=Get&Browser=NETSCAPE4">');
              document.write('</'+'scr'+'ipt>');
              document.write(adcode); }
         else if (browVersion>=3)
              { document.write(''); } }
    if (browName=='Microsoft Internet Explorer')
         { document.write('<ifr'+'ame src="http://ad.html.it/banman.asp?ZoneID=36&Task=Get&X=' + browDateTime + '" width=468 height=60 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No></ifr'+'ame>'); }
    // -->
    </script>
    <noscript>
        <a href="http://ad.html.it/banman.asp?ZoneID=36&Task=Click&Mode=HTML&PageID=7414" target="_new">
        </a>
    </noscript>
    </div>
    
    <div id="divDescription">
     </div>
    
    </font></BODY>
    </HTML>
    Per chi può interessare posto il codice dello script in JavaScript che ho riprodotto vedendo l'applet:
    codice:
    <html>
    <head>
    	<title>Menu</title>
    </head>
    <body>
    
    <script type="text/javascript">
    <!--
    
    var voceMenu=new Array();
     voceMenu[0]="Home page";
     voceMenu[1]="Forum";
     voceMenu[2]="Mailing list";
     voceMenu[3]="Javascript";
     voceMenu[4]="Applet Java";
     voceMenu[5]="Sondaggi";
    
    var oggettoMenu=new Array();
    
    var altezza=23;
    var larghezza=100;
    var id_contenitore="contenitore";
    
    // metodo decolora
    function decolora() {
     document.getElementById(this.nomeOggettoHTML).style.backgroundColor="pink";
    };
    
    // metodo getLeft
    function getLeft() {
     var getLeft=parseInt(document.getElementById(this.nomeOggettoHTML).style.left);
     return getLeft;
    };
    
    // metodo mouseOver
    function mouseOver() {
     if(eval("window."+this.nomeOggetto+".timer")) clearTimeout(this.timer);
     document.getElementById(this.nomeOggettoHTML).style.backgroundColor="orange";
     document.getElementById(this.nomeOggettoHTML).style.left="40px";
    };
    
    // metodo mouseOut
    function mouseOut() {
     if(this.getLeft()>0) {
      document.getElementById(this.nomeOggettoHTML).style.left=this.getLeft()-1;
     }
     else {
      clearTimeout(this.timer);
     };
     this.timer=window.setTimeout(this.nomeOggetto+".mouseOut();",20);
    };
    
    // classe Menu
    function Menu(numeroArray) {
     this.nomeOggetto="oggettoMenu["+numeroArray+"]";
     this.nomeOggettoHTML="subMenu"+numeroArray;
     this.numeroArray=numeroArray;
     this.testo=voceMenu[this.numeroArray];
     this.timer=null;
     this.mouseOver=mouseOver;
     this.mouseOut=mouseOut;
     this.decolora=decolora;
    };
    
    // funzione costruzioneSubMenu
    function costruzioneSubMenu() {
     for(var x=0; x<voceMenu.length; x++) {
      document.getElementById(id_contenitore).innerHTML+="<div id=\""+oggettoMenu[x].nomeOggettoHTML+"\" style=\"position:absolute; top:"+(altezza*oggettoMenu[x].numeroArray)+"px; left:0px; width:100px; height:"+altezza+"px; background-color:pink; border:#ffffff 2px outset;\" onmouseover=\""+oggettoMenu[x].nomeOggetto+".mouseOver();\" onmouseout=\""+oggettoMenu[x].nomeOggetto+".decolora(); "+oggettoMenu[x].nomeOggetto+".mouseOut();\">"+oggettoMenu[x].testo+"</div>";
      oggettoMenu[x].getLeft=getLeft;
     };
    };
    
    // funzione inizia
    function inizia() {
     for(var x=0; x<voceMenu.length; x++) oggettoMenu[x]=new Menu(x);
     costruzioneSubMenu();
    };
    
    // funzione all'avvio
    window.onload=inizia;
    
    // costruzione contenitore
    document.write("<div id=\""+id_contenitore+"\" style=\"position:absolute; top:20px; left:20px; border:#000000 0px solid; width:250px; height:250px; color:#ffffff; font:bold 1pc arial; text-align:center\"> </div>");
    
    //-->
    </script>
    
    </body>
    </html>
    Grazie a chi mi risponderà.

  2. #2
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    ...

    up

  3. #3
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    ...

    up

  4. #4
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    ...

    Riposto il codice del mio script con alcune nuove modifiche!

    codice:
    <html>
    <head>
    	<title>Menu</title>
    </head>
    <body>
    
    <script type="text/javascript">
    <!--
    
    var voceMenu=new Array();
     voceMenu[0]=["Home page","./index.htm"];
     voceMenu[1]=["Forum","./forum.htm"];
     voceMenu[2]=["Mailing list","./mailing.htm"];
     voceMenu[3]=["Javascript","./javascript.htm"];
     voceMenu[4]=["Applet Java","./applet.htm"];
     voceMenu[5]=["Sondaggi","./sondaggi.htm"];
    
    var oggettoMenu=new Array();
    
    var altezza=23;
    var altezzaContenuto=27;
    var larghezza=100;
    var id_contenitore="contenitore";
    
    // metodo vaiURL
    function vaiURL() {
     window.location.href=this.URL;
    };
    
    // metodo decolora
    function decolora() {
     document.getElementById(this.nomeOggettoHTML).style.backgroundColor="pink";
    };
    
    // metodo getLeft
    function getLeft() {
     var getLeft=parseInt(document.getElementById(this.nomeOggettoHTML).style.left);
     return getLeft;
    };
    
    // metodo mouseOver
    function mouseOver() {
     if(eval("window."+this.nomeOggetto+".timer")) clearTimeout(this.timer);
     document.getElementById(this.nomeOggettoHTML).style.backgroundColor="orange";
     document.getElementById(this.nomeOggettoHTML).style.left="40px";
    };
    
    // metodo mouseOut
    function mouseOut() {
     if(this.getLeft()>0) {
      document.getElementById(this.nomeOggettoHTML).style.left=this.getLeft()-1;
     }
     else {
      clearTimeout(this.timer);
     };
     this.timer=window.setTimeout(this.nomeOggetto+".mouseOut();",20);
    };
    
    // classe Menu
    function Menu(numeroArray) {
     this.nomeOggetto="oggettoMenu["+numeroArray+"]";
     this.nomeOggettoHTML="subMenu"+numeroArray;
     this.numeroArray=numeroArray;
     this.testo=voceMenu[this.numeroArray][0];
     this.URL=voceMenu[this.numeroArray][1];
     this.timer=null;
     this.mouseOver=mouseOver;
     this.mouseOut=mouseOut;
     this.decolora=decolora;
     this.vaiURL=vaiURL;
    };
    
    // funzione costruzioneSubMenu
    function costruzioneSubMenu() {
     for(var x=0; x<voceMenu.length; x++) {
      document.getElementById(id_contenitore).innerHTML+="<div style=\"position:absolute; top:"+(oggettoMenu[x].numeroArray*altezzaContenuto)+"px; left:0px; width:100px; height:"+altezzaContenuto+"px; border:#000000 0px solid; cursor:hand\" onmouseover=\""+oggettoMenu[x].nomeOggetto+".mouseOver();\" onmouseout=\""+oggettoMenu[x].nomeOggetto+".decolora(); "+oggettoMenu[x].nomeOggetto+".mouseOut();\" onclick=\""+oggettoMenu[x].nomeOggetto+".vaiURL();\"><div id=\""+oggettoMenu[x].nomeOggettoHTML+"\" style=\"position:absolute; top:0px; left:0px; width:100px; height:"+altezza+"px; background-color:pink; border:#ffffff 1px outset\">"+oggettoMenu[x].testo+"</div>";
      oggettoMenu[x].getLeft=getLeft;
     };
    };
    
    // funzione inizia
    function inizia() {
     for(var x=0; x<voceMenu.length; x++) oggettoMenu[x]=new Menu(x);
     costruzioneSubMenu();
    };
    
    // funzione all'avvio
    window.onload=inizia;
    
    // costruzione contenitore
    document.write("<div id=\""+id_contenitore+"\" style=\"position:absolute; top:20px; left:20px; border:#000000 0px solid; width:250px; height:250px; color:#ffffff; font:bold 1pc arial; text-align:center\"> </div>");
    
    //-->
    </script>
    
    </body>
    </html>

  5. #5
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    la parte che non capisci sembrerebbe non avere alcun utilizzo, 2 ipotesi
    [poco probabile] - hack (che personalmente ignoro) per mantenere la compatibilità con NN4 e/o mac
    [più probabile] - parte di un qualche debug dimenticato nel codice
    ciao

  6. #6
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    ...

    puoi essere piu chiaro?
    A me sembra un riferimento a se stesso.
    Se facio un window.setTimeout: window.setTimeout(this.obj+".metodo();",20); fa riferimento a se stesso? come mai????

  7. #7
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    questo
    codice:
    this.obj = obj + "Object";
    eval(this.obj + "=this");
    non ha apparentemente ragione d' esservi

    se non ti interessava sapere questo spiegati meglio tu

  8. #8
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    ...

    Ho trovato lo script che fa a caso nostro, ti commento nel codice le parti che non capisco:

    codice:
    <HTML>
    
    <HEAD>
    <TITLE>HTML.it - il sito italiano sul Web publishing | Un JavaScript a settimana | HTML.it</TITLE>
    
    <style>
    A:link, A:visited { text-decoration: none }
    A:hover { text-decoration: underline }
    </style>
    
    <style type="text/css">
    #divMenu0{position:absolute; top:0; left:30; visibility:hidden; font-family:verdana,helvetica; font-size:15px; font-weight:bold}
    #divMenu1{position:absolute; top:0; left:150; visibility:hidden; font-family:verdana,helvetica; font-size:15px; font-weight:bold}
    #divMenu2{position:absolute; top:0; left:350; visibility:hidden; font-family:verdana,helvetica; font-size:15px; font-weight:bold}
    </style>
    <script language="JavaScript" type="text/javascript">
    
    
    function lib_bwcheck(){
    	this.ver=navigator.appVersion
    	this.agent=navigator.userAgent
    	this.dom=document.getElementById?1:0
    	this.opera5=this.agent.indexOf("Opera 5")>-1
    	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
    	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
    	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
    	this.ie=this.ie4||this.ie5||this.ie6
    	this.mac=this.agent.indexOf("Mac")>-1
    	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
    	this.ns4=(document.layers && !this.dom)?1:0;
    	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
    	return this
    }
    var bw=new lib_bwcheck()
    
    
    var tMove=10;
    
    
    var tSpeed=40
    
    
    var tMoveOnScroll=true
    
    
    var tShow=20
    
    
    function makeMenu(obj,nest,show,move,speed){
        nest=(!nest) ? "":'document.'+nest+'.'
    	this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0;
      	this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0;
    	this.x=this.css.left||this.css.pixelLeft||this.el.offsetLeft||0
    	this.y=this.css.top||this.css.pixelTop||this.el.offsetTop||0
    	this.state=1; this.go=0; this.mup=b_mup; this.show=show; this.mdown=b_mdown;
    	this.height=bw.ns4?this.css.document.height:this.el.offsetHeight
    	this.moveIt=b_moveIt; this.move=move; this.speed=speed
    
    
    
    /* parte che non capisco */
        this.obj = obj + "Object"; 	eval(this.obj + "=this")
    /* parte che non capisco */
    
    
    
    }
    function b_moveIt(x,y){this.x=x; this.y=y; this.css.left=this.x; this.css.top=this.y}
    
    function b_mup(){
    	if(this.y>-this.height+this.show){
    		this.go=1; this.moveIt(this.x,this.y-this.move)
    		setTimeout(this.obj+".mup()",this.speed)
    	}else{this.go=0; this.state=1}
    }
    
    function b_mdown(){
    	if(this.y<eval(scrolled)){
    		this.go=1; this.moveIt(this.x,this.y+this.move)
    
    
    
    /* parte che non capisco */
    		setTimeout(this.obj+".mdown()",this.speed)
    /* parte che non capisco */
    
    
    
    
    	}else{this.go=0; this.state=0}
    }
    
    function moveTopMenu(num){
    	if(!oMenu[num].go){
    		if(!oMenu[num].state)oMenu[num].mup()
    		else oMenu[num].mdown()
    	}
    	for(i=0;i<oMenu.length;i++){
    		if(i!=num && !oMenu[i].state){ oMenu[i].mup()}
    	}
    }
    
    function checkScrolled(){
    	for(i=0;i<oMenu.length;i++){
    		if(!oMenu[i].go){
    			y=!oMenu[i].state?eval(scrolled):eval(scrolled)-oMenu[i].height+oMenu[i].show
    			oMenu[i].moveIt(oMenu[i].x,y)
    		}
    	}
    	if(bw.ns4||bw.ns6) setTimeout('checkScrolled()',40)
    }
    
    function topMenuInit(){
    	oMenu=new Array()
    	oMenu[0]=new makeMenu('divMenu0',"",20,10,20)
    	oMenu[1]=new makeMenu('divMenu1',"",tShow,tMove,tSpeed)
    	oMenu[2]=new makeMenu('divMenu2',"",20,10,20)
    	scrolled=bw.ns4||bw.ns6?"window.pageYOffset":"document.body.scrollTop"
    
    	for(i=0;i<oMenu.length;i++){
    		oMenu[i].moveIt(oMenu[i].x,-oMenu[i].height+oMenu[i].show)
    		oMenu[i].css.visibility='visible'
    	}
    	if(tMoveOnScroll) bw.ns4||bw.ns6?checkScrolled():window.onscroll=checkScrolled;
    }
    
    
    onload=topMenuInit;
    </script>
    
    </HEAD>
    <BODY  marginleft="0" marginheight="0" BGCOLOR="white" vlink="blue" link="blue">
    
    
    
    <div id="divMenu0">
    	Link1
    
    
    	Link2
    
    
    	Link3
    
    
    	Link4
    
    
    	MENU 1
    </div>
    <div id="divMenu1">
    	[img]01.gif[/img]
    
    	[img]02.gif[/img]
    
    	MENU 2
    </div>
    <div id="divMenu2">
    	Prova
    
    	Prova con scritta senza link
    
    
    
    	MENU 3
    </div>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    </div>
    
    <font size=1 face="arial">
    <div align="center">
    <SCRIPT LANGUAGE="JAVASCRIPT">
    <!--
    var browName = navigator.appName;
    var browDateTime = (new Date()).getTime();
    var browVersion = parseInt(navigator.appVersion);
    var ua=navigator.userAgent.toLowerCase();
    var adcode='';
    if (browName=='Netscape'){
         if (browVersion>=5)
              { document.write('<ifr'+'ame src="http://ad.html.it/banman.asp?ZoneID=36&Task=Get&Browser=NETSCAPE6&X=' + browDateTime + '" width=468 height=60 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No></ifr'+'ame>'); }
         else if ((browVersion>=4)&&(ua.indexOf("mac")==-1))
              { document.write('<S'+'CRIPT src="http://ad.html.it/banman.asp?ZoneID=36&Task=Get&Browser=NETSCAPE4">');
              document.write('</'+'scr'+'ipt>');
              document.write(adcode); }
         else if (browVersion>=3)
              { document.write(''); } }
    if (browName=='Microsoft Internet Explorer')
         { document.write('<ifr'+'ame src="http://ad.html.it/banman.asp?ZoneID=36&Task=Get&X=' + browDateTime + '" width=468 height=60 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No></ifr'+'ame>'); }
    // -->
    </script>
    <noscript>
        <a href="http://ad.html.it/banman.asp?ZoneID=36&Task=Click&Mode=HTML&PageID=7414" target="_new">
        </a>
    </noscript>
    </div>
    
    <div id="divDescription">
     </div>
    
    </font></BODY>
    </HTML>

  9. #9
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    ...

    up

  10. #10
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    scusa, sopra ho detto e ripetuto una boiata
    purtroppo non sono molto ferrato in oop, suppongo che con
    this.obj = obj + "Object";
    la proprietà obj contenga una stringa composta dal nome passato al costruttore + "Object"
    e che con
    eval(this.obj + "=this")
    questa stringa diventi una variabile globale utilizzabile altrove come riferimento all' oggetto in questione
    (come nel caso del Timeout da te segnalato)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.