How to use Javascript inside

Started by business, February 22, 2006, 10:20:02 PM

Previous topic - Next topic

business

Hi,
I want to know how to insert javascript inside index.templates.php because when ever i use the javascript, my forum will cause "Template Parse Error"
Actually i want to use image map just like shown inside quote below. o yes i also do not forget to put those html inside echo'....';
Quoteecho '
<map name="Map">
  <area shape="rect" coords="87,7,147,37" href="/index.htm">
  <area shape="rect" coords="157,7,242,37" href="/menu.htm" onclick="return show_hide_box(this, 200, 70, '2px solid');">
  <area shape="rect" coords="330,8,427,35" href="hxxp:www.businesseducationsite.com/osticket/ [nonactive]">
</map>';
in above quote you will see syntax "onclick". if i insert this syntax....my forum will error. however if I don't use is my forum will run correctly. syntax onclick will call javascript function as shown below:

Quote
<SCRIPT TYPE="text/javascript"><!--
/* Script by: hxxp:www.jtricks.com [nonactive]
* Version: 20060202
* Latest version:
* hxxp:www.jtricks.com/javascript/window/box.html [nonactive]
*/
// Moves the box object to be directly beneath an object.
function move_box(an, box)
{
    var cleft = 200;
    var ctop = 55;
    var obj = an;

    while (obj.offsetParent)
    {
        cleft += obj.offsetLeft;
        ctop += obj.offsetTop;
        obj = obj.offsetParent;
    }

    box.style.left = cleft + 'px';

    ctop += an.offsetHeight + 8;

    // Handle Internet Explorer body margins,
    // which affect normal document, but not
    // absolute-positioned stuff.
    if (document.body.currentStyle &&
        document.body.currentStyle['marginTop'])
    {
        ctop += parseInt(
            document.body.currentStyle['marginTop']);
    }

    hxxp:box.style.top [nonactive] = ctop + 'px';
}

// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is curre
function show_hide_box(an, width, height, borderStyle)
{
    var href = an.href;
    var boxdiv = document.getElementById(href);

    if (boxdiv != null)
    {
        if (boxdiv.style.display=='none')
        {
            // Show existing box, move it
            // if document changed layout
            move_box(an, boxdiv);
            boxdiv.style.display='block';
        }
        else
            // Hide currently shown box.
            boxdiv.style.display='none';
        return false;
    }

    // Create box object through DOM
    boxdiv = document.createElement('div');

    // Assign id equalling to the document it will show
    boxdiv.setAttribute('id', href);

    boxdiv.style.display = 'block';
    boxdiv.style.position ='absolute';
    boxdiv.style.width = width + 'px';
    boxdiv.style.height = height + 'px';
    boxdiv.style.border = borderStyle;

    var contents = document.createElement('iframe');
    contents.scrolling = 'no';
    contents.frameBorder = '0';
    contents.style.width = width + 'px';
    contents.style.height = height + 'px';

    boxdiv.appendChild(contents);

    document.body.appendChild(boxdiv);
    move_box(an, boxdiv);

    if (contents.contentWindow)
        contents.contentWindow.document.location.replace(
            href);
    else
        contents.src = href;

    // The script has successfully shown the box,
    // prevent hyperlink navigation.
    return false;
}
//--></SCRIPT>


However how to make this script run without error?
anyone can help me?

Dannii

"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise."

business

Sorry i don't get what do you mean? i am not programmer, i just know html.
which quote do you mean? and how to write it?
Thanks

Dannii

echo '
<map name="Map">
  <area shape="rect" coords="87,7,147,37" href="/index.htm">
  <area shape="rect" coords="157,7,242,37" href="/menu.htm" onclick="return show_hide_box(this, 200, 70, \'2px solid\');">
  <area shape="rect" coords="330,8,427,35" href="http://www.businesseducationsite.com/osticket/">
</map>';
When you echoed the text, it tried to do it between the single quotes, however because you used them inside, it didn't know what to do with the 2px solid part.
"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise."

business

Thanks, i already put the onclick inside the map tag.

Quoteonclick="return show_hide_box(this, 200, 70, \'2px solid\');

and it is working.

however when I put the javascript code (inside script tag), the forum error again. should i add "\" for every single quote that appear in that script?

Dannii

The big script is also in the index.template? Yes if it is.
You could consider putting it in a separate file and linking to it though.
"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise."

business

Thanks a lot, my forum already work correctly.

Advertisement: