Avatar billede theselfimages Nybegynder
22. juli 2004 - 17:20 Der er 16 kommentarer og
1 løsning

Javascript tilføjelse til ASP random funktion

Kan man tilføje en Javascript funktion til nedenstående:

<%
Randomize
'Get random number
i = int(rnd*4) + 1
%>

Hvis 'i' kommer ud som = "4", så sættes en ny værdi et andet sted:

F.eks.:
IF i="4" then
  margin-left = "425"
Else
  margin-left = "474"
End if

denne <%=margin-left%> sidder i body

Det skal køre client side, så det skal lave som Javascript!
But how?
Avatar billede theselfimages Nybegynder
22. juli 2004 - 17:25 #1
Eller måske bare lave hele random funktionen som Javascript?
Avatar billede powernodes Nybegynder
22. juli 2004 - 17:46 #2
<script type="text/javascript">
window.onload = function() {
  var number = Math.random()*4
  if (Math.round(number) == 4) {
    document.body.style.marginLeft = "425px";
  } else {
    document.body.style.marginLeft = "474px";
  }
}
</script>
Avatar billede powernodes Nybegynder
22. juli 2004 - 17:50 #3
..og kan det bruges kan det skrives lidt smartere:

<script type="text/javascript">
window.onload = function() {
  document.body.style.marginLeft=(Math.round(Math.random()*4)==4)?"425px":"474px";
}
</script>
Avatar billede theselfimages Nybegynder
22. juli 2004 - 18:01 #4
OK!.. har prøvet.. men hvad er der galt her?

            <script type="text/javascript">
        window.onload = function() {
        var num = Math.random()*4
        if (Math.round(num) == 4) {
        document.write('<div style='position:absolute;margin-top:7px;margin-left:425px;'><img border='0' src='images/front/top4.jpg'></div><div style='position:absolute;margin-top:132px;margin-left:0px;'><img border='0' src='images/front/main4.jpg'></div>');
        } else {
        document.write('<div style='position:absolute;margin-top:7px;margin-left:474px;'><img border='0' src='images/front/top' + num + '.jpg'></div><div style='position:absolute;margin-top:132px;margin-left:0px;'><img border='0' src='images/front/main' + num + '.jpg'></div>');
          }
        }
          </script>
Avatar billede powernodes Nybegynder
22. juli 2004 - 18:08 #5
Sådan her:

<script type="text/javascript">
window.onload = function() {
  var num = Math.random()*4;
  if (Math.round(num) == 4) {
    document.write('<div style="position:absolute;margin-top:7px;margin-left:425px;">444444<img border="0" src="images/front/top4.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main4.jpg"></div>');
  } else {
    document.write('<div style="position:absolute;margin-top:7px;margin-left:474px;">NOOOOO<img border="0" src="images/front/top' + num + '.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main' + num + '.jpg"></div>');
  }
}
</script>
Avatar billede powernodes Nybegynder
22. juli 2004 - 18:09 #6
hov, "44444" og "NOOOO" er lige en test af min - slet dem hellere :)
Avatar billede theselfimages Nybegynder
22. juli 2004 - 18:19 #7
Hmm! Det her virker ikke.. får bare en blank side, som om det ikke document writer noget som heltst!

<script type="text/javascript">
window.onload = function() {
  var num = Math.random()*4;
  if (Math.round(num) == 4) {
    document.write('<td style="position:relative;top:0px;left:0px;z-index: 10;"><div id="skjulDiv"><div style="position:absolute;margin-top:7px;margin-left:425px;"><img border="0" src="images/front/top4.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main4.jpg"></div><div style="position:absolute;margin-top:225px;margin-left:-219px;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="500" HEIGHT="50"><PARAM NAME=movie VALUE="images/flash/frise.swf"><PARAM NAME=menu VALUE=false><PARAM NAME=quality VALUE=best><PARAM NAME=wmode VALUE=transparent><EMBED SRC="images/flash/frise.swf" MENU=false QUALITY=best WIDTH="500" HEIGHT="50" WMODE=transparent TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT></div></div></td>');
  } else {
    document.write('<td style="position:relative;top:0px;left:0px;z-index: 10;"><div id="skjulDiv"><div style="position:absolute;margin-top:7px;margin-left:474px;"><img border="0" src="images/front/top' + num + '.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main' + num + '.jpg"></div><div style="position:absolute;margin-top:225px;margin-left:-219px;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="500" HEIGHT="50"><PARAM NAME=movie VALUE="images/flash/frise.swf"><PARAM NAME=menu VALUE=false><PARAM NAME=quality VALUE=best><PARAM NAME=wmode VALUE=transparent><EMBED SRC="images/flash/frise.swf" MENU=false QUALITY=best WIDTH="500" HEIGHT="50" WMODE=transparent TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT></div></div></td>');
  }
}
}
</script>
Avatar billede powernodes Nybegynder
22. juli 2004 - 18:21 #8
Nu ved jeg ikke hvad du vil, eller hvorfor du bruger document.write.
I de fleste tilfælde ville adde til innerHTML da være at fortrække, som sådan fx:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload = function() {
  var num = Math.random()*4;
  if (Math.round(num) == 4) {
    document.body.innerHTML += '<div style="position:absolute;margin-top:7px;margin-left:425px;"><img border="0" src="images/front/top4.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main4.jpg"></div>';
  } else {
    document.body.innerHTML += '<div style="position:absolute;margin-top:7px;margin-left:474px;"><img border="0" src="images/front/top' + num + '.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main' + num + '.jpg"></div>';
  }
}
</script>
</head>
<body>
<p>Dette findes i forvejen</p>
</body>
</html>
Avatar billede powernodes Nybegynder
22. juli 2004 - 18:22 #9
Du har en } for meget.
Avatar billede theselfimages Nybegynder
22. juli 2004 - 18:22 #10
Den skriver en masse decimaler ud på billedstien?
main2.45454657656576857685.jpg
Avatar billede theselfimages Nybegynder
22. juli 2004 - 18:35 #11
Det virker ikke.. det her er hvad jeg har... men der skrev ingenting:


<!--DIV LAYERS -->
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="770">
<tr>

<td style="position:relative;top:0px;left:0px;z-index: 10;"><div id="skjulDiv">
         
<script type="text/javascript">
window.onload = function() {
  var num = Math.random()*4;
  if (Math.round(num) == 4) {
    document.body.innerHTML += '<div style="position:absolute;margin-top:7px;margin-left:425px;"><img border="0" src="images/front/top4.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main4.jpg"></div>';
  } else {
    document.body.innerHTML += '<div style="position:absolute;margin-top:7px;margin-left:474px;"><img border="0" src="images/front/top' + num + '.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main' + num + '.jpg"></div>';
  }
}
</script>         
         
<div style="position:absolute;margin-top:225px;margin-left:-219px;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="500" HEIGHT="50"><PARAM NAME=movie VALUE="images/flash/frise.swf"><PARAM NAME=menu VALUE=false><PARAM NAME=quality VALUE=best><PARAM NAME=wmode VALUE=transparent>
<EMBED SRC="images/flash/frise.swf" MENU=false QUALITY=best WIDTH="500" HEIGHT="50" WMODE=transparent TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT></div></div></td>
     
</tr>
</table>
<!--DIV LAYERS -->
Avatar billede powernodes Nybegynder
22. juli 2004 - 18:50 #12
Dette virker fint hos mig:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload = function() {
  var target=document.getElementById("target"),num=Math.random()*4;
  if (Math.round(num)==4) {
    target.innerHTML+='<td style="position:relative;top:0px;left:0px;z-index: 10;"><div id="skjulDiv"><div style="position:absolute;margin-top:7px;margin-left:425px;"><img border="0" src="images/front/top4.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main4.jpg"></div><div style="position:absolute;margin-top:225px;margin-left:-219px;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="500" HEIGHT="50"><PARAM NAME=movie VALUE="images/flash/frise.swf"><PARAM NAME=menu VALUE=false><PARAM NAME=quality VALUE=best><PARAM NAME=wmode VALUE=transparent><EMBED SRC="images/flash/frise.swf" MENU=false QUALITY=best WIDTH="500" HEIGHT="50" WMODE=transparent TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT></div></div></td>';
  } else {
    target.innerHTML+='<td style="position:relative;top:0px;left:0px;z-index: 10;"><div id="skjulDiv"><div style="position:absolute;margin-top:7px;margin-left:474px;"><img border="0" src="images/front/top' + num + '.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main' + num + '.jpg"></div><div style="position:absolute;margin-top:225px;margin-left:-219px;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="500" HEIGHT="50"><PARAM NAME=movie VALUE="images/flash/frise.swf"><PARAM NAME=menu VALUE=false><PARAM NAME=quality VALUE=best><PARAM NAME=wmode VALUE=transparent><EMBED SRC="images/flash/frise.swf" MENU=false QUALITY=best WIDTH="500" HEIGHT="50" WMODE=transparent TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT></div></div></td>';
  }
}
</script>
</head>
<body>

<button onclick="alert(document.body.innerHTML)">Kilde</button>

<div id="target">
<table border="1" cellspacing="0" cellpadding="0">
  <tr id="target"></tr>
</table></div>

</body>
</html>

Læg mærke til at jeg wrapper en div med samme id som target udenom tabellen. Uden dette giver IE runtime error. Firefox, Opera etc behøver den ikke.
Avatar billede powernodes Nybegynder
22. juli 2004 - 18:58 #13
Nej vent lidt.
En anden gang ville det være en del lettere hvis du gad forklare hvad du vil have fra STARTEN - og ikke henad vejen.

Nu har vi rodet rundt i innerHTML vs. document.write etc etc., men uden stadig helt at kunne se hvad du vil have, tror jeg dette rammer tæt på:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<button onclick="alert(document.body.innerHTML)">Kilde</button>

<table border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>STATISK TEKST</td>
  </tr>
  <tr>
    <script type="text/javascript">
    var num=Math.random()*4;
    if (Math.round(num)==4) {
      document.write('<td style="position:relative;top:0px;left:0px;z-index: 10;"><div id="skjulDiv"><div style="position:absolute;margin-top:7px;margin-left:425px;"><img border="0" src="images/front/top4.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main4.jpg"></div><div style="position:absolute;margin-top:225px;margin-left:-219px;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="500" HEIGHT="50"><PARAM NAME=movie VALUE="images/flash/frise.swf"><PARAM NAME=menu VALUE=false><PARAM NAME=quality VALUE=best><PARAM NAME=wmode VALUE=transparent><EMBED SRC="images/flash/frise.swf" MENU=false QUALITY=best WIDTH="500" HEIGHT="50" WMODE=transparent TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT></div></div></td>');
    } else {
      document.write('<td style="position:relative;top:0px;left:0px;z-index: 10;"><div id="skjulDiv"><div style="position:absolute;margin-top:7px;margin-left:474px;"><img border="0" src="images/front/top' + num + '.jpg"></div><div style="position:absolute;margin-top:132px;margin-left:0px;"><img border="0" src="images/front/main' + num + '.jpg"></div><div style="position:absolute;margin-top:225px;margin-left:-219px;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="500" HEIGHT="50"><PARAM NAME=movie VALUE="images/flash/frise.swf"><PARAM NAME=menu VALUE=false><PARAM NAME=quality VALUE=best><PARAM NAME=wmode VALUE=transparent><EMBED SRC="images/flash/frise.swf" MENU=false QUALITY=best WIDTH="500" HEIGHT="50" WMODE=transparent TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT></div></div></td>');
    }
    </script>
  </tr>
</table>

</body>
</html>
Avatar billede theselfimages Nybegynder
22. juli 2004 - 20:39 #14
OK! Sorry! Lidt dårligt forklaret.. Det virker næsten nu!
prøv at se her: http://test2.theselfimages.dk

Klik F5 indtil du ser en sejlbåd. Det er den faste (altså denne: <img border="0" src="images/front/top4.jpg">) .. men tilsyneladende sætter den ikke 1,2,3 værdier ind her: <img border="0" src="images/front/top' + num + '.jpg"> og her <img border="0" src="images/front/main' + num + '.jpg">
Avatar billede theselfimages Nybegynder
22. juli 2004 - 20:46 #15
OK! Fik det selv til at virke:

Ændrede denne:
var num=Math.random()*4;

til:
var num = Math.ceil(Math.random()*4);

Tak for hjælpen, selv om spørgsmålet var lidt uklart!
Post et svar!
Avatar billede powernodes Nybegynder
23. juli 2004 - 08:47 #16
okay cool :)
Avatar billede theselfimages Nybegynder
23. juli 2004 - 09:28 #17
Takker
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester