run in javascript

0

I would like to know how I do so that javascript executes the following but without the need for the button, which is automatic when I open the page

<!DOCTYPE>

<html>
 
<head>
 
  <SCRIPT language=JavaScript>
	
function ejemplo2()
  {


document.write('<td rowspan=2>');
var num = Math.random(); 
 var imagen = Math.floor(Math.random() * 2); 
 var nombre = "C:/Users/Vivi/Downloads/TAROT/fondo/imagenes/"+imagen+".png";
 var texto = texto_aleatorio[imagen](); 
 document.write("<IMG NAME=portada SRC="+nombre+" width=280 height=450 BORDER=2>"); 
document.write('&nbsp');
document.write('</td>');

}


var texto_aleatorio = [];

texto_aleatorio[0] = function() { 
   var textos = new Array(); 
   textos[0] = "Tenemos los mejores productos del mercado, con controles de    calidad intensivos."; 

   textos[1] = "Distribuimos en todo el mundo con los mejores tiempos de    entrega y fiabilidad de los envíos."; 

   textos[2] = "No tenemos competidores que nos hagan sombra. Contrate con    nosotros y compuébelo. Así de fácil.";
 
   textos[3] = "Disponga del mejor servicio de atención al cliente y una    respuesta rápida a sus problemas."; 

   var aleat = Math.random()  * (textos.length); 
   aleat = Math.floor(aleat); 


document.write(textos[aleat]) 
}

texto_aleatorio[1] = function(){ 
   var textos = new Array();
   textos[0] = " mejores productos del mercado, con controles de    calidad intensivos.";
   textos[1] = " en todo el mundo con los mejores tiempos de    entrega y fiabilidad de los envíos."; 
   textos[2] = " competidores que nos hagan sombra. Contrate con    nosotros y compuébelo. Así de fácil."; 
   textos[3] = " del mejor servicio de atención al cliente y una    respuesta rápida a sus problemas."; 

   var aleat = Math.random() * (textos.length);
   aleat = Math.floor(aleat);


document.write(textos[aleat]) 
}


		
</SCRIPT>
  

</head>

 <body>

       
      <button onclick=ejemplo2()  value="empezar">empezar</button>
       
                 

 
 </body>
</html>
    
asked by Jasbleidy Medina 26.08.2017 в 22:28
source

3 answers

0

How are you using native javascript you just have to use window.onload = ejemplo2(); here I leave the clear example of what you have and what you need

function ejemplo2() {
  document.write('<td rowspan=2>');
  var num = Math.random();
  var imagen = Math.floor(Math.random() * 2);
  var nombre = "C:/Users/Vivi/Downloads/TAROT/fondo/imagenes/" + imagen + ".png";
  var texto = texto_aleatorio[imagen]();
  document.write("<IMG NAME=portada SRC=" + nombre + " width=280 height=450 BORDER=2>");
  document.write('&nbsp');
  document.write('</td>');
}


var texto_aleatorio = [];

texto_aleatorio[0] = function() {
  var textos = new Array();
  textos[0] = "Tenemos los mejores productos del mercado, con controles de    calidad intensivos.";
  textos[1] = "Distribuimos en todo el mundo con los mejores tiempos de    entrega y fiabilidad de los envíos.";
  textos[2] = "No tenemos competidores que nos hagan sombra. Contrate con    nosotros y compuébelo. Así de fácil.";
  textos[3] = "Disponga del mejor servicio de atención al cliente y una    respuesta rápida a sus problemas.";
  var aleat = Math.random() * (textos.length);
  aleat = Math.floor(aleat);
  document.write(textos[aleat]);
}

texto_aleatorio[1] = function() {
  var textos = new Array();
  textos[0] = " mejores productos del mercado, con controles de    calidad intensivos.";
  textos[1] = " en todo el mundo con los mejores tiempos de    entrega y fiabilidad de los envíos.";
  textos[2] = " competidores que nos hagan sombra. Contrate con    nosotros y compuébelo. Así de fácil.";
  textos[3] = " del mejor servicio de atención al cliente y una    respuesta rápida a sus problemas.";
  var aleat = Math.random() * (textos.length);
  aleat = Math.floor(aleat);
  document.write(textos[aleat])
}

window.onload = ejemplo2();
    
answered by 26.08.2017 / 22:53
source
0

If you use jQuery , you can launch it when the document is ready:

$(document).ready(función(){
  ejemplo2();
});

Another option is to do it in the event onload of the body, for example

<body onload="ejemplo2()">
    
answered by 26.08.2017 в 22:45
0

You can do it in 2 ways, using onload in the <body> tag or by placing <javascript> within the html code, example:

option 1:

Remove the Button and replace the <body> tag with:

<body onload="ejemplo2()">

option 2:

replace the button:

<button onclick=ejemplo2()  value="empezar">empezar</button>

by:

<javascript>ejemplo2()</javascript>
    
answered by 27.08.2017 в 20:53