Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: onclick syntax

  1. #1
    _Q_
    _Q_ is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onclick syntax

    Hi,

    I have an infuriating 'object expected' error, that I just cant find. Perhaps a new perspective could shed light on it. I have two images (left and right arrows) to which I have attached onclick commands. But when I click on them, I get the 'object expected' error on the following lines:

    Code:
    <img  src="./?f=101103"   onclick="moveSlideLeft(); return true;"/>
    <img src="./?f=101104"   onclick="moveSlideRight(); return true;"/>
    The onclick commands should jump to the commands below. I put in some alerts at this point. Its not even reaching these functions, so I dont think the immeadiate error lies within updateSlide(), so I wont crowd you with the code here. I guess that the problem must be how I'm calling the moveSlideLeft/Right functions from the onclick property.

    <script>
    function moveSlideLeft(){
    updateSlide(-1);
    }

    function moveSlideRight(){
    updateSlide(1);
    }
    </script>
    -->

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Should work... Anyway there is no need to return true. And probably you need to specify the type of your script code

    <script type="text/javascript">

    Wouldn't be normal a straight trigger?:

    <img src="./?f=101103" onclick="updateSlide(-1)"/>
    <img src="./?f=101104" onclick="updateSlide(1)"/>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    _Q_
    _Q_ is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The only reason I had the two wrapping functions was in case the -1 and 1 weren't being parsed properly or something... I had what you've suggested earlier. I expect both ways work and the problem lies elsewhere.

    Maybe its got to do with where my script is in the document... I'm working within a Content Management System called MySource. I have a script file which is referenced in the header of each page, but I didnt put this script in it (the one for updateSlide) because it only applies to one page. I figured it made more sense to locate the function near the object that it relates to, rather than being lost in a huge javascript file.

    Is that a problem?

    -Q

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I figured it made more sense to locate the function near the object that it relates to, rather than being lost in a huge javascript file.
    Nope, this is not the problem, except maybe if the code is far away bellow the element and the event try to trigger a function which was not loaded, if the action is quickly enough. Most of the time javascript codes are to be placed in the HEAD of the document, for this reason.

    But it looks like you might encounter the same problem which apperas when trying to insert a javascript code in a .tpl page (using Smarty, for instance). In that case, the javscript code must be marked as a "literal"

    {literal}
    <script type="text/javascript">
    ...some code here...
    </script>
    {/literal}

    I have never worked with MySource, so that I just presumed that it might be a similar problem.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    _Q_
    _Q_ is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The {literal} thing doesnt work, it just prints it onto the page.

    I checked it in FireFox and found that the javascript console says that the problem is that moveSlideLeft and moveSlideRight are not defined...

    But the script that does define them is above where I state the mouseover property(!)

    Should I use a different syntax to define the functions?

    -Q

  • #6
    _Q_
    _Q_ is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Opera gives the following message:

    Code:
    Event thread: click
    Error:
    name: ReferenceError
    message: Statement on line 1: Reference to undefined variable: moveSlideLeft
    Backtrace:
      Line 1 of  script 
        moveSlideLeft();


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •