Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript]Wywołanie funkcji
stefik4
post
Post #1





Grupa: Zarejestrowani
Postów: 176
Pomógł: 1
Dołączył: 18.11.2007

Ostrzeżenie: (0%)
-----


Witam posiadam funkcje JS którą chaiał bym aby uruchamiała się wrac z załadowaniem strony. Próbowałem body onload ale nie działa ;/

Spróbowałem wpisać nazwę i też nic:

  1. <script type="text/javascript">
  2. var dialogWidth = 300;
  3. var dialogHeight = 200;
  4. var timeout = 10000;
  5. function showModal(dW, dH)
  6. {
  7. var dialogDiv = document.getElementById("dialogDiv");
  8. var transparentDiv = document.getElementById("transparentDiv");
  9.  
  10. if(!dialogDiv) return;
  11. if(!transparentDiv) return;
  12. if(!dW) var dW = dialogWidth;
  13. if(!dH) var dH = dialogHeight;
  14.  
  15. dialogDiv.style.width = dW + "px";
  16. dialogDiv.style.height = dH + "px";
  17.  
  18. var clientWidth = parseInt(document.documentElement.clientWidth);
  19. var clientHeight = parseInt(document.documentElement.clientHeight);
  20. var scrollHeight = parseInt(document.documentElement.scrollHeight);
  21. var scrollWidth = parseInt(document.documentElement.scrollWidth);
  22.  
  23. transparentDiv.style.width = Math.max(scrollWidth, clientWidth) + "px";
  24. transparentDiv.style.height = Math.max(scrollHeight, clientHeight) + "px";
  25.  
  26. var left = Math.floor((clientWidth - dW) / 2);
  27. var top = Math.floor((clientHeight - dH) / 2);
  28.  
  29. dialogDiv.style.top = top -400+ "px";
  30. dialogDiv.style.left = left + "px";
  31.  
  32. dialogDiv.style.display = "block";
  33. transparentDiv.style.display = "block";
  34. }
  35. function closeModal()
  36. {
  37. var dialogDiv = document.getElementById("dialogDiv");
  38. var transparentDiv = document.getElementById("transparentDiv");
  39.  
  40. if(!dialogDiv) return;
  41. if(!transparentDiv) return;
  42.  
  43. dialogDiv.style.display = "none";
  44. transparentDiv.style.display = "none";
  45. }
  46. showModal(500,280);
  47. </script>


A gdy w treści strony dodam odnośnik: <a href="#" onclick="showModal(550, 290);">.</a> to funkcja otworzy okno i jest okej.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
Earth
post
Post #2





Grupa: Zarejestrowani
Postów: 169
Pomógł: 2
Dołączył: 6.12.2006

Ostrzeżenie: (0%)
-----


window.onload
Go to the top of the page
+Quote Post
stefik4
post
Post #3





Grupa: Zarejestrowani
Postów: 176
Pomógł: 1
Dołączył: 18.11.2007

Ostrzeżenie: (0%)
-----


Kod
      window.onload = showModal;


nie działa.
Go to the top of the page
+Quote Post
zegarek84
post
Post #4





Grupa: Zarejestrowani
Postów: 1 332
Pomógł: 294
Dołączył: 12.10.2008
Skąd: Olkusz

Ostrzeżenie: (0%)
-----


w kliku na linku przekazałeś parametry - w onload nie podałeś żadnych parametrów [wygląda to tak jakbyś uruchomił funkcję showModal()] - w sumie nie zawsze trzeba przekazywać [poza tym akurat tutaj inaczej musiałbyś je przekazać np. przez funkcję anonimową jeśli już byś nic rozsądniejszego nie mógł wymyślić] - można je wcześniej zdefiniować - ale musiałbyś rozumieć zasięg zmiennych w javascript - wielu twierdzi, że to dziwny język - ale jak się zrozumie podstawowe zasady zaskoczyć może prostotą, łatwością definiowania prywatnych zmiennych, hermetyzacją, zmiennymi chronionymi [zaraz napiszą, że nie ma czegoś takiego ^^ ;p]

podsumowując bezsensowny skrót myślowy:
window.onload = function(){showModal(550, 290);};

ale lepiej poczytaj o zasięgu zmiennych - a zanim zrozumiesz javascript zapamiętaj jedno - funkcje tutaj są obiektami także, ale z kolei obiekty nie są funkcjami - mogą mieć zestawy metod [funkcji] i innych parametrów ale to nie obiekty...

Ten post edytował zegarek84 23.11.2010, 22:25:31
Go to the top of the page
+Quote Post
stefik4
post
Post #5





Grupa: Zarejestrowani
Postów: 176
Pomógł: 1
Dołączył: 18.11.2007

Ostrzeżenie: (0%)
-----


  1. <script type="text/javascript">
  2.  
  3. var dialogWidth = 300;
  4. var dialogHeight = 200;
  5. var timeout = 10000;
  6. function showModal(dW, dH)
  7. {
  8. var dialogDiv = document.getElementById("dialogDiv");
  9. var transparentDiv = document.getElementById("transparentDiv");
  10.  
  11. if(!dialogDiv) return;
  12. if(!transparentDiv) return;
  13. if(!dW) var dW = dialogWidth;
  14. if(!dH) var dH = dialogHeight;
  15.  
  16. dialogDiv.style.width = dW + "px";
  17. dialogDiv.style.height = dH + "px";
  18.  
  19. var clientWidth = parseInt(document.documentElement.clientWidth);
  20. var clientHeight = parseInt(document.documentElement.clientHeight);
  21. var scrollHeight = parseInt(document.documentElement.scrollHeight);
  22. var scrollWidth = parseInt(document.documentElement.scrollWidth);
  23.  
  24. transparentDiv.style.width = Math.max(scrollWidth, clientWidth) + "px";
  25. transparentDiv.style.height = Math.max(scrollHeight, clientHeight) + "px";
  26.  
  27. var left = Math.floor((clientWidth - dW) / 2);
  28. var top = Math.floor((clientHeight - dH) / 2);
  29.  
  30. dialogDiv.style.top = top -400+ "px";
  31. dialogDiv.style.left = left + "px";
  32.  
  33. dialogDiv.style.display = "block";
  34. transparentDiv.style.display = "block";
  35. }
  36. function closeModal()
  37. {
  38. var dialogDiv = document.getElementById("dialogDiv");
  39. var transparentDiv = document.getElementById("transparentDiv");
  40.  
  41. if(!dialogDiv) return;
  42. if(!transparentDiv) return;
  43.  
  44. dialogDiv.style.display = "none";
  45. transparentDiv.style.display = "none";
  46. }
  47.  
  48. window.onload = function(){showModal(550, 290);};
  49. </script>


nie działa. Już wszystkie opcje probowałem i wciąz nic.
Go to the top of the page
+Quote Post
blade-mrn
post
Post #6





Grupa: Zarejestrowani
Postów: 113
Pomógł: 11
Dołączył: 20.10.2009

Ostrzeżenie: (0%)
-----


to powinno być chyba tak:
window.onload = showModal(550, 290);
a nie tak:
window.onload = function(){showModal(550, 290);};
Po co to function przecież ty wywołujesz funkcję a nie definiujesz nową.
Go to the top of the page
+Quote Post
stefik4
post
Post #7





Grupa: Zarejestrowani
Postów: 176
Pomógł: 1
Dołączył: 18.11.2007

Ostrzeżenie: (0%)
-----


blade_mrn, nie działa ;/
Go to the top of the page
+Quote Post
konrados
post
Post #8





Grupa: Zarejestrowani
Postów: 623
Pomógł: 79
Dołączył: 16.01.2008

Ostrzeżenie: (0%)
-----


No a co mówi konsola blędów? Ctrl+Shift+J w firefoxie.
Jak coś nie działa w js, to tam zaglądamy i sprawdzamy co nie działa.
Go to the top of the page
+Quote Post
stefik4
post
Post #9





Grupa: Zarejestrowani
Postów: 176
Pomógł: 1
Dołączył: 18.11.2007

Ostrzeżenie: (0%)
-----


(IMG:http://upload.webds.pl/pictures/17779.png)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 19:38