#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    155
    Rep Power
    13

    Javascript function call works from href but not onclick


    Hi all. I'm working on a content mangement system, and on the page where users can edit their pages, there exists a link which takes them to a menu editing page.

    If the user clicks this link, I need to remind the user that their changes will be lost if they do not save, and if they confirm, I redirect to the menu page, if not the function returns false and they go nowhere.

    Here's the function:

    Code:
    function comfirm_page_mod_exit(url) {
    	var check = confirm('Message to user');
    	if (check) {
    		document.location.href = url;
    	} else {
    		return false;
    	}
    }
    Now, if I call the function like this:

    Code:
    <a href="Javascript:confirm_page_mod_exit('url');">
    it works fine, but when I try this:
    Code:
    <a href="Javascript:;" onclick="confirm_page_mod_exit('url');">
    I get the confirm box, but if I select "OK" nothing happens.

    Does anyone have any idea why this is happening?

    Thanks in advance,
    Pablo
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    It's happening because the default action of the link - loading the (empty) javascript&#58; url - is occurring. This interferes with the setting of the location programmatically (should be [window.]location, btw). You can avoid this by adding 'return false' to the onclick code, or replacing the href with '#'. Better way:
    Code:
    <a href="http://www.google.com" onclick="return confirm('Go to google?')">google</a>
    This won't leave your JS-disabled users out in the cold.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    155
    Rep Power
    13
    Gracias, Adios.

    Your solution worked perfectly.

    I'm lucky in that I don't have to worry about JS-disabled users in this case, though, as the CMS I'm building requires IE5.5 or greater because of the WYSIWYG editor I've incorporated into it. If anyone here doesn't like windows, they can use terminal services, or go to one of the PC labs to edit their pages.

    Simple as that ;o)

    And I actually have to use javascript in this case, as the menu which will be edited after the user clicks the link will depend on what is selected in a select list next to the link. The URL to be fetched is built dynamically when the link is clicked.

    Thanks again.

    Pablo
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    1
    Rep Power
    0

    Unhappy Say what?


    I'm a bit confused, how do you make the javascript action the DEFAULT using the code described above?

    Yes I want people who have disabled JAVASCRIPT to be able to use my site, but I've worked hard on what I have and to my knowledge, JAVASCRIPT is the only way to go...

    If you look at http://www.partysite.com you'll be greeted with a javascript enabled page that allows me to direct a user based on then links on the left, but they must first plug in a zipcode...

    Using the code above, the javascript never gets executed BECAUSE there is something in the href...

    Any suggestions?

    Really all I want to be able to do is send the form data without having lots of ugly little buttons, is there a better way to do this WITHOUT using Javascript?

    Scott...
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    101
    To make a javascript command "default"...

    Code:
    <a href="javascript:someFunction();">Click here to run some JavaScript</a>
    However "Java Script" is supposed to be one word. I don't know why this forum puts a space in between them, but it isn't supposed to be there.
    Last edited by Clone53421; June 20th, 2003 at 05:08 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    155
    Rep Power
    13
    I would suspect it's to somehow prevent the submission of potentially malicious code, though since code submitted seems to be automatically translated to html entities, I'm not sure how one could be malicious. Oh well..........;o)

    P.
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    101
    My thinking exactly... although you couldmake a URL tag have a javascript href, they could just break "javascript" if it was in the URL. It's kind of annoying to try to post code and have it put a space in there, particularly if I can tell that the person I'm replying to probably doesn't know enough about it to know the space doesn't belong there.

IMN logo majestic logo threadwatch logo seochat tools logo