Simple Machines Community Forum

SMF Support => SMF 2.0.x Support => Aiheen aloitti: me1982 - maaliskuu 06, 2011, 10:20:56 AP

Otsikko: Insert JavaScript into index.template.php
Kirjoitti: me1982 - maaliskuu 06, 2011, 10:20:56 AP
Hi folks,

I hope somebody can help me. I added some javascriptcode to my index.template.php, to open a new window on a link click. I did it like this:
Lainaa

// Output any remaining HTML headers. (from mods, maybe?)
   echo $context['html_headers'];

   echo '
   <script type="text/javascript">function open_win()
      {
         window.open(
            "somesiteof.php","sitename","width=400,heigt=400"
         );
      }
   </script>
</head>
<body>';
}

It only opens the window, but doesn't compute any of the specs / attributes. If I change it to this:
Lainaa

   // Output any remaining HTML headers. (from mods, maybe?)
   echo $context['html_headers'];

   echo '
   <script type="text/javascript">function open_win()
      {
         window.open(
            'somesiteof.php','sitename','width=400,heigt=400'
         );
      }
   </script>
</head>
<body>';
}
,

like w3schools is advising to do it, the webpage doesn't show any of the theme anymore. Plus, most of the content is just gone.

So, what would be the right way to do it? It is supposes to result in a link that can open a window, which is fix in its size.

The linkcode looks like this:
Lainaa
<a href="javascript:open_win();">link</a>


I use smf 2.0 rc5.

Greetz!
Otsikko: Re: Insert JavaScript into index.template.php
Kirjoitti: Matthew K. - maaliskuu 06, 2011, 10:33:36 AP
For the second method, single quotes should be escaped. Find: ' Replace: \'
Otsikko: Re: Insert JavaScript into index.template.php
Kirjoitti: ascaland - maaliskuu 06, 2011, 10:46:56 AP
Dont forget to fix that spelling mistake: heigt.
Otsikko: Re: Insert JavaScript into index.template.php
Kirjoitti: me1982 - maaliskuu 06, 2011, 10:48:10 AP
Found the spelling misstake already. thx for the hint.

Works, thx a lot. Is this php related?
Otsikko: Re: Insert JavaScript into index.template.php
Kirjoitti: Arantor - maaliskuu 06, 2011, 10:50:02 AP
The whole issue of the ' is SMF/PHP related, because the code is being sent from PHP, and in PHP both ' and " have significance - you can't use a ' inside a '...' block without putting \ in front of it (much as in Javascript)

The benefit of using ' in Javascript is fairly minimal compared to " anyway though. It's more significant in PHP.
Otsikko: Re: Insert JavaScript into index.template.php
Kirjoitti: me1982 - maaliskuu 06, 2011, 11:04:22 AP
Thx.

I worked the whole day on the page which should go into the new window, to realize, that it doesnt work with my routine. so thanks a lot guys. works pretty fine and i am satisfied with the result.