Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

drk7

[Resolvido] Script só roda com carregamento no body

Recommended Posts

Olhem esse script:

 

<!--

SlideShow with Captions and Cross-Fade
(C)2002 by CodeLifter.com

Shows images and accompanying captions.
Browsers: NS4-7,IE4-6
Fade effect only in IE; degrades gracefully.
NS4 shows default caption only.

INSTRUCTIONS:

Copy this entire script into a completely blank
page.  Follow the commented instructions within.

//-->

<html>

<head>

<!-- 
Set up the caption font in the following style.
Place the style script in the head of the page.
//-->

<style>
.Caption {
font-family: Arial;
font-weight: bold;
color:      #123456;
}
</style>

<!-- 
Place the following script in the head of the page.
Follow the set-up instructions within the script.
//-->

<script>


		



// (C) 2002 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this header.

// ==============================
// Set the following variables...
// ==============================

// Set the slideshow speed (in milliseconds)
var SlideShowSpeed = 3000;

// Set the duration of crossfade (in seconds)
var CrossFadeDuration = 3;

var Picture = new Array(); // don't change this
var Caption = new Array(); // don't change this

// ESPECIFIQUE SUAS IMAGENS NOS NOMES ABAIXO
// Specify the image files...
// To add more images, just continue
// the pattern, adding to the array below.
// To use fewer images, remove lines
// starting at the end of the Picture array.
// Caution: The number of Pictures *must*
// equal the number of Captions!

Picture[1]  = 'img1.jpg';
Picture[2]  = 'img2.jpg';
Picture[3]  = 'img3.jpg';
Picture[4]  = 'img4.jpg';
Picture[5]  = 'img5.jpg';

// ESPECIFIQUE A LEGENDA DE CADA IMAGEM
// Specify the Captions...
// To add more captions, just continue
// the pattern, adding to the array below.
// To use fewer captions, remove lines
// starting at the end of the Caption array.
// Caution: The number of Captions *must*
// equal the number of Pictures!

Caption[1]  = "This is the first caption.";
Caption[2]  = "This is the second caption.";
Caption[3]  = "This is the third caption.";
Caption[4]  = "This is the fourth caption.";
Caption[5]  = "This is the fifth caption.";

// =====================================
// Do not edit anything below this line!
// =====================================

var tss;
var iss;
var jss = 1;
var pss = Picture.length-1;

var preLoad = new Array();
for (iss = 1; iss < pss+1; iss++){
preLoad[iss] = new Image();
preLoad[iss].src = Picture[iss];}

function runSlideShow(){
if (document.all){
document.images.PictureBox.style.filter="blendTrans(duration=2)";
document.images.PictureBox.style.filter="blendTrans(duration=CrossFadeDuration)";
document.images.PictureBox.filters.blendTrans.Apply();}
document.images.PictureBox.src = preLoad[jss].src;
if (document.getElementById) document.getElementById("CaptionBox").innerHTML= Caption[jss];
if (document.all) document.images.PictureBox.filters.blendTrans.Play();
jss = jss + 1;
if (jss > (pss)) jss=1;
tss = setTimeout('runSlideShow()', SlideShowSpeed);
}

</script>

</head>

<!--
Add the onload=runSlideShow() event call to the body tag.
//-->

<body  bgcolor=#000000>
<script type="JavaScript">runSlideShow()</script>
<!--
The following table holds the images and captions.
Place the table in your page where you want the slideshow
to appear.  Follow the instructions for each table cell.
//-->

<table border=0 cellpadding=0 cellspacing=0>
  <tr>
    <!--
    The next table cell holds the images.
    Set cell and image width and height the same.
    The img src must have name=PictureBox in its
    tag.  Usually the first image in the Picture
    array in the script is used here.
    //-->
    <td width=350 height=280>
    <img src=img1.jpg name=PictureBox width=350 height=280>
    </td>
  </tr>
  <tr>
    <!-- 
    The next table cell holds the captions.
    This table cell must have id=CaptionBox and
    class=Caption in its tag. The default caption
    shows whilst loading in all browsers; NS4
    will show only the default caption, throughout.
    //-->
    <td id=CaptionBox class=Caption align=center bgcolor=#fedcba>
    This is the default caption.
    </td>
  </tr>
</table>

</body>

</html>

O problema: ele só funciona no body onload. Rodando dali, ele dá problema com outros scripts que tenho. Se tento rodar ele chamando na primeira linha após o body

<script language="JavaScript">runSlideShow();</script>
ele carrega aquele default caption e não exibe imagem nenhuma (além de ficar parado).

 

OBS: Aqui só coloquei o script principal, omitindo os outros scripts, já que eles não são o problema.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Primeiramente, separe isso num arquivo JS em separado.

 

Depois, crie uma função load() por exemplo e faça assim:

 

window.onload = load;

Dentro dessa função load() você cola td que deve ser carregado na página.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Oi continua sem funcionar, porque isso daria na mesma. O que eu quero: fazer esse script funcionar sem ter que carregar no <body onload=...> que no momento é o único jeito de fazêlo rodar. Ou funciona também criando um botão e no onclick chamar a função. Mas eu queria que ele fosse iniciado de forma automática, só que sem ser no <body onload>. O outro script que dá problema é iniciado sem precisar fazer chamada a ele.

 

Eu sempre separo o script em um arquivo separado, é que por enquanto ele está um "protótipo".

Compartilhar este post


Link para o post
Compartilhar em outros sites

eu queria que ele fosse iniciado de forma automática, só que sem ser no <body onload>. O outro script que dá problema é iniciado sem precisar fazer chamada a ele.

 

E como você pretende que ele 'inicie' automaticamente?

você precisa de alguma coisa para disparar, e no caso o window.onload, eh um evento muito bom para fazer isso.

 

Qual eh o real problema? eu nao conheco forma de iniciar um script, sem chamar ele.

Nao faz nem sentido, uma coisa executar, sem que você mande..

Compartilhar este post


Link para o post
Compartilhar em outros sites

Queria iniciar assim, como eu tnha colocado antes:

//body
<script language="Javascript">runSlideShow();</script>

 

Só que desse jeito não funciona. Não consigo enxergar o problema.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Cara, era só fazer igual ao que o Rick.hjpbarcelos disse no POST 2...

 

Segue um trecho de como ficaria seu código (veja a chamada do evento na 4ª linha):

if (jss > (pss)) jss=1;
tss = setTimeout('runSlideShow()', SlideShowSpeed);
}
window.onload = runSlideShow;
</script>
</head>
<body bgcolor=#000000>
<table border=0 cellpadding=0 cellspacing=0>
  <tr>

Veja também que o body não contém evento onload. Isso irá resolver o seu problema. http://forum.imasters.com.br/public/style_emoticons/default/natal_happy.gif

Compartilhar este post


Link para o post
Compartilhar em outros sites

Ficou na mesma. Eu não estou conseguindo enxergar o porquê. Se ele é carregado inicialmente, o outro script (um rte, que varre textareas e insere o editor nelas) não carrega no Firefox. Já no IE é o contrário: carrega o editor, mas o slide de imagens não funciona.

 

EDIT: Na verdade com o carregamento no próprio js, está igual tanto no IE como no FF: não carrega o rte.

 

EDIT2: Acho que eu achei o problema: O editor já carrega automaticamente em um window.onload. Acho que agora vai!

 

Problema: fiz assim:

 

function run()
{
	var oldOnload = window.onload;

	if (typeof(window.onload) != "function")
	{
		window.onload = widgInit, runSlideShow;
	}
	else
	{
		window.onload = function()
		{
			oldOnload();
			widgInit();
		}
	}
}

run();

Como carregar 2 coisas no window.onload ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Não precisa de 'tudo isso', basta listar oque você quer carregar numa unica chamada, assim:

window.onload = function()
{
   funcao1();
   funcao2();
   funcao3();
   funcao4();
   runSlideShow();
   widgInit();
}

você que fez essa função run() ?

se fizer como acima, não precisa dela.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.