PDA

View Full Version : Can Javascript be run from the plugin system?


JPnyc
12-06-2006, 07:49 PM
I did search on this but got no hits. Can javascript be run from the plugin system, or just php? I have a script that I want run onload of the showthread template. The onload event handler is contained within the script. I tried it on various showthread hooks, but no dice. I know the script works, since I've placed it into the showthread template to test, and it works.

Kirk Y
12-07-2006, 12:52 AM
Not to my knowledge, no. AFAIK, Plugins can only be used to execute PHP Code.

Guest190829
12-07-2006, 01:02 AM
Yes, you can add html and javascript to the headinclude variable before the template is called.

JPnyc
12-07-2006, 03:41 AM
Ah so the <script> tags should be included? What if I want the script to run from the body instead of the head of the doc? It's only needed on the showthread template, so to put it into the headinclude seems overkill. Also, for some reason, when included in the head, the script doesn't work, when moved to the body, it does. Obviously some conflict with some other script in the head.

Paul M
12-07-2006, 04:18 AM
You can just code the plugin to only add it to headinclude if showthread is being called (allthough that won't fix your second problem of it not working).

JPnyc
12-07-2006, 01:14 PM
Right, for some reason it only functions when outside the head of the doc. I might have to hardcode it into the template as I've been doing. The plugin system is a tremendous step forward for companies like ours who run 30 forums in vb format. When we go to upgrade, being able to just upload all the customizations rather than adding them by hand, is just a godsend.

JPnyc
11-29-2007, 11:08 AM
Sorry for reviving an old thread, but it was mine to begin with so I hope no one minds, and the subject is still the same. I need to execute some Javascript from the plugin system and not sure how to go about it. Can I use PHP to spit out or execute the JS? If so, would echo or eval be used? Can I just put a javascript directly into the plugin system and have it execute on a hook? Thanks

Marco van Herwaarden
11-29-2007, 11:33 AM
Plugins are executing PHP and are executed server-side.

JavaScript is embeded in them HTML page, and is executed on the client-side. You can put JS in your templates, use echo (echo is not advised in plugins) or assign your JS code to a variable in a plugin and include that variable in a template. You can however never execute JS in a plugin as JS does not execute on the server-side.

JPnyc
11-29-2007, 12:11 PM
okay thanks, but I could use PHP in a plug-in to write a JavaScript function to the page, on a given hook, could I not? I mean passing it through PHP as a string literal.

We run 30 forums, and I'm looking for a way to have these alterations be part of the plug-in system, so that I can just upload an XML file rather than making manual changes to templates on 30 forums. Thanks

RedTyger
11-29-2007, 12:33 PM
The attached product should do what you want. When installed, the source code of showthread looks like this, changes highlighted in bold:


<script type="text/javascript" src="clientscript/vbulletin_global.js?v=367"></script>
<script type="text/javascript" src="clientscript/vbulletin_menu.js?v=367"></script>

<!-- END TEMPLATE: headinclude --><script type="text/javascript" src="clientscript/yourscript.js"></script>



and


<body onload="do_js();">



You'll have to edit the product to change what javascript is added, of course. Make sure the semicolon at the end of the onload bit is left, sometimes other onload javascript is included too, apparently.

RedTyger
11-29-2007, 12:36 PM
You'll probably want to change the title to something other than vB Ad Management as well, I forgot. :)

ragtek
11-29-2007, 12:38 PM
instead of body onload => use
window.onload=..... so you have not to edit the body tag ;)

RedTyger
11-29-2007, 12:42 PM
Also good. Come to that, showthread's onload javascript is a variable so you could probably tack additional onload stuff onto the end as well. Instead of a str_replace.

Choices choices. :)

JPnyc
11-29-2007, 12:43 PM
THANK YOU!!!! Actually, ad management is precisely what I'm trying to do. I want to enable the forums Ajax features, however we make our living off ad revenue, and as you know, Ajax doesn't reload the page, so we lose significantly on ad delivery. What I'm trying to do is reload the ad positions along with the edit and quick reply Ajax events.

RedTyger
11-29-2007, 12:48 PM
Something like the option to refresh adverts every X seconds, does that sound worth doing? It's not what you're trying to achieve, but generally speaking...?

JPnyc
11-29-2007, 01:01 PM
I'm not sure we can actually do that, but I'll check. The problem is we are serving our ads via PHP, meaning PHP is writing the JavaScript setup code to the page. I haven't been able to figure out away to refresh the variable call by hooking it on the Ajax start and Ajax complete hooks. That would be ideal, if I could do that way.

So what I've been trying to do, in lieu of that, is dynamically write the JavaScript function that calls the ad, by using JavaScript onclick of the edit and quick reply submit buttons. So I'm writing one JavaScript function to the page with another, using innerHTML, because it's easier than using the DOM methods in this instance.

--------------- Added 1196354702 at 1196354702 ---------------

RedTyger, I looked over that plugin you graciously attached. Am I correct that it adds a link to a remote JavaScript located in the client script directory, and calls it on page load?

JPnyc
12-07-2007, 11:49 AM
FYI, this issue is resolved. I ended up writing my own AJAX script to refresh our ad positions, and calling it onclick of the quick edit and quick reply buttons. Unfortunatly, the quick edit SAVE button is dynamically written and I can't get at it to add the onclick event. But 2 out of 3 ain't bad...

RedTyger
12-08-2007, 09:13 AM
Oh, good job. I ended up adding a timed AJAX advertisement refresh to my ad manager off the back of this, though its not really related. Seems to be appreciated. :)