javascript in plugin

Third Party Plugins to CMSimple - how to install, use and create plugins

Moderator: Tata

Post Reply
maeg
Posts: 525
Joined: Fri Feb 20, 2009 2:27 pm
Location: Agerbæk, Denmark
Contact:

javascript in plugin

Post by maeg » Fri Nov 08, 2013 8:46 pm

Hi

As i understand i can include a javascript in the head of the template $hjs like

Code: Select all

$hjs = '<script type="text/javascript" src="'.$pth['folder']['plugins'].'xxxx/xxxxx.js"></script>'."\n";
And just before the </body> with $bjs like

Code: Select all

$bjs = '<script type="text/javascript" src="'.$pth['folder']['plugins'].'xxxxx/xxxxx.js"></script>'."\n";
The $hjs works fine, but i can't get the $bjs to work.

If i have understand it correct, $bjs = '<script type="text/javascript" src="'.$pth['folder']['plugins'].'xxxx/xxxxx.js"></script>'."\n"; will give me something like this

Code: Select all

<body>
some html
<script type="text/javascript">
    some javascript code
</script>
</body
</html>
If i'm correct i can't understand why i dosen't work for me :x

Pahaps someone like to take a look at me code!

Have a nice day

Best regards
Jens
aka maeg

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: javascript in plugin

Post by cmb » Fri Nov 08, 2013 9:09 pm

Hi Jens,

in both cases ($hjs and $bjs) it is important to append the script to the variable instead of overwriting it, so you have to use .= instead of =:

Code: Select all

$hjs .= '...';
$bjs .= '...';
maeg wrote:If i have understand it correct, $bjs = '<script type="text/javascript" src="'.$pth['folder']['plugins'].'xxxx/xxxxx.js"></script>'."\n"; will give me something like this
[...]
You have understood correctly. However, there was a bug with regard to $bjs that was only fixed in CMSimple_XH 1.5.7. So you should use a newer version.
maeg wrote:If i'm correct i can't understand why i dosen't work for me :mad:
Maybe you are using this code inside of a function. Then $hjs resp. $bjs have to be "declared" global (otherwise the assignment has no effect):

Code: Select all

function whatever()
{
    global $hjs;

    $hjs .= '...';
} 
If that's not the case, you can send me your complete code, and I'll have a look at it.

Christoph
Christoph M. Becker – Plugins for CMSimple_XH

maeg
Posts: 525
Joined: Fri Feb 20, 2009 2:27 pm
Location: Agerbæk, Denmark
Contact:

Re: javascript in plugin

Post by maeg » Fri Nov 08, 2013 10:17 pm

cmb wrote: Maybe you are using this code inside of a function. Then $hjs resp. $bjs have to be "declared" global (otherwise the assignment has no effect):

Code: Select all

function whatever()
{
    global $hjs;

    $hjs .= '...';
} 
If that's not the case, you can send me your complete code, and I'll have a look at it.
Ahhhhh... fogot to declared it global
Now it work as i want.

I just have a littel problem with the code to call the plugin.

When i call the plugin form the template i have to use this code

Code: Select all

<?php scroll();?><a href='#' id='scroll-to-top' style='display:none;'></a>
then it works. You can see it on cmsimple-xh.dk when you scroll a littel down

I'm trying to put the code

Code: Select all

<a href='#' id='scroll-to-top' style='display:none;'></a>
inside the plugin, so i only have to call

Code: Select all

<?php scroll();?>
But everly time i get a white screen. :cry:

Any good ideers??

Jens

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: javascript in plugin

Post by cmb » Fri Nov 08, 2013 10:52 pm

maeg wrote:You can see it on http://cmsimple-xh.dk when you scroll a littel down
Ah, nice one! :)
maeg wrote:But everly time i get a white screen.
That's most likely caused by a syntax error. To write the HTML to a string, you have to properly escape the apostrophes (') with a backslash:

Code: Select all

$o .= '<a href=\'#\' id=\'scroll-to-top\' style=\'display:none;\'></a>'; 
Alternatively (that's what I personally prefer, usually) use quotes (") for the attribute values:

Code: Select all

$o .= '<a href="#" id="scroll-to-top" style="display:none;"></a>';
A tip for plugin development: always activate the debug mode and work in admin mode; then you'll see some helpful notices and warnings, and rather rarely have a white screen. :)
Christoph M. Becker – Plugins for CMSimple_XH

maeg
Posts: 525
Joined: Fri Feb 20, 2009 2:27 pm
Location: Agerbæk, Denmark
Contact:

Re: javascript in plugin

Post by maeg » Fri Nov 08, 2013 11:02 pm

Hi

It is funny, i have tryed that code 25 times where i did't work. But now when i copy the code from your replay and paste it in it works :D

Thx

Jens

lck
Posts: 2967
Joined: Wed Mar 23, 2011 11:43 am
Contact:

Re: javascript in plugin

Post by lck » Sun Nov 10, 2013 1:48 pm

Hi Jens,
nice plugin. I currently looking for exactly this plugin. Can we download somewhere?

Ludwig
„Bevor du den Pfeil der Wahrheit abschießt, tauche die Spitze in Honig!“   👉 Ludwig's XH-Templates for MultiPage & OnePage

Post Reply