0

I am trying to be able to load JavaScript function when I load an article inside Joomla. In normal HTML coding that would be in the body using the onload event - there I would be able to load what ever I want including JS functions. i.e.

<body onload="myJSFunction()">

I am using Sorcerer to be able to write down the JS inside Joomla AS IS - But I could NOT be able to load the function when the article loads.

I would be happy to know the right way to load JS functions in Joomla Articles. I have managed to activate the JS using an OnClick event - that means the JS code works inside Joomla.

I would like that the event would be activated on Article load - Is there a way to do that? Is there a PHP code that can assist me with that? I have tried using this code with no luck - When My Article ID is 133. That code should have change the Joomla body to use the onload event when it meets Article ID number 133 - But, It did not work in Joomla at all and I did not get the onload event I wanted.

> <?php if ($_GET['id'] == 133) {
>       $body = '<body onload="MyJSFunction()">';
>       } else {
>              $body = '<body>';
>       } ?>

Please assist me with making this happen. Thanks in Advanced

1 Answer 1

1

If you are able to put javascript into the document, then you could add a line that attaches the function to the onload event using document.body.addEventListener.

Something like

document.body.addEventListener("onload", function(){ MyJSFunction(); });

Your function could live inside the anonymous function itself.

Modifying this after our discussion (and correcting a typo):

<script type="text/javascript">
document.body.addEventListener("onload", function(){
    var firstDivContent = document.getElementById('MyDiv');
    var secondDivContent = document.getElementById('MyDiv2');
    secondDivContent.innerHTML = firstDivContent.innerHTML;
});
</script>

or if there are no other event listeners around, you could try:

<body onload="var firstDivContent = document.getElementById('MyDiv');
    var secondDivContent = document.getElementById('MyDiv2');
    secondDivContent.innerHTML = firstDivContent.innerHTML;">
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Matiu. I have tried adding your code inside the current Joomla code that I have - like this as well: <script type="text/javascript">document.body.addEeventListener("onload", function(){ doSomething(); }); </script> <script type="text/javascript"> function doSomething(){ ...... } </script> BUT it didn't work - The JSFunction worked just fine using the OnClick event. But I could not make it work onload event. Any idea why? thanks
if you put your function inside the addEventListener, then you can be sure it exists when called. Sometimes javascript gets all mixed up about what is present and not when loading, so you might find that you are getting some kind of "no such function" error. The function you call has to exist when the event listener is added to the body.
Thanks. I am not quite understand what I need to do? I have switched the places and still only worked on the onClick event and no Onload. and when I did this <script type="text/javascript">document.body.addEeventListener("onload", function doSomething(){ var firstDivContent = document.getElementById('MyDiv'); var secondDivContent = document.getElementById('MyDiv2'); secondDivContent.innerHTML = firstDivContent.innerHTML; }; }); </script> It DID NOT work at all. Please help me understand what I am doing wrong. Thanks
I had a typo which you have copied (corrected now), and there was an extra }; in your code. See the new version of my answer -hope it helps.
Thanks Not work used {source}<span style="font-family: courier new, courier, monospace;"><br />&lt;script type="text/javascript"&gt;<br />document.body.addEventListener("onload", function(){<br /><img src="/pets/media/sourcerer/images/tab.png" alt="" />var firstDivContent = document.getElementById('MyDiv');<br /><img src="/pets/media/sourcerer/images/tab.png" alt="" />var secondDivContent = document.getElementById('MyDiv2');<br /><img src="/pets/media/sourcerer/images/tab.png" alt="" />secondDivContent.innerHTML = firstDivContent.innerHTML;<br />});<br />&lt;/script&gt;<br /></span>{/source}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.