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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Newbie Javascript Questions

    I have a few buttons on a page I'm developing and I want the onclick event to call a javascript function in an external file and execute it. I would like to be able to pass a parameter to that function and then either have the function take the user to a new URL or make changes to the webpage content.

    Initially (just to test) I had inline javascript that caused an alert to popup. That worked fine. Next I took the inline code and put it in a function in an external javascript file that was referenced in the HTML:

    Code:
    <script type="text/javascript" src="../assets/js/ews-js.js"></script> 
    
     <a link="www.xx.com" class="sk-shop-button" onClick="onclick_shop_button">Shop for Supplies</a>
    The function is as follows:
    Code:
    function onclick_shop_button() {
       window.alert('Life Is Good!'); 
       return false;
    }
    When I attempt to call it this way nothing happens. I'm guessing I should not be using a link there since I have to build the URL inside the Javascript (for now though the Javascript is just showing an alert).

    In one use case I want to be able to pass a parameter which would contain a product name so the function could load the correct webpage. So the function would have to build the URL using the product name as part of the URL then load that page.

    In a different use case (for a different button) the user would click on an EDIT button that would then update parts of the webpage allowing the user to click on areas to delete content.

    I am not sure how to implement either use case. I know I can use CSS to modify the look of the webpage and then use subsequent onclick events to do the deleting but am not sure how to delete HTML from within Javascript. Same with how to go to a new URL from within Javascript. Finally I am not sure why the inline Javascript works but the external does not.

    Any help would be GREATLY appreciated!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,983
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    You should study JavaScript at www.w3schools.com where you will find answers to these (and many other) questions. This forum is not really the appropriate place for general questions.

    To answer your specific question, either your external script is not in the correct directory, or it contains invalid code such as HTML tags.

    Quote Originally Posted by bobvaun View Post
    Finally I am not sure why the inline Javascript works but the external does not.
    As devnull69 has pointed out, the code you have posted does not work regardless of the location of the script.
    Last edited by Philip M; 07-06-2011 at 04:57 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    1. The "link" is supposed to be an href attribute
    2. The call to onclick_shop_button needs the parentheses ... onclick="onclick_shop_button()"

  • #4
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    1. The "link" is supposed to be an href attribute
    2. The call to onclick_shop_button needs the parentheses ... onclick="onclick_shop_button()"
    Thanks for pointing this out. I had it right at one time but in messing around I left out the parentheses. That did get the code called.


  •  

    Posting Permissions

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