SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems with Confirm Function - User Error, JavaScript Error or Server Error?

    My client has a php based bulletin board application on an internal server. It is set up so that users can post to the board and delete the post at their leisure. There is a delete page that the user visits that calls a Confirm() JavaScript function to give the user a chance to stop from accidentally deleting the post. If the user confirms the action, the post is deleted. If they cancel, they are taken to the "Edit Post" page.

    Just recently, the function stopped working for the client. He gets a script error in Firefox:
    Message: Object expected
    Line: 210
    Char: 1
    Code: 0
    URI:*XXXXXXXXXXXXXXXXXXXXXXXXXX


    I cannot seem to replicate this issue at all. The application runs just like it is supposed to on my test server (Xampp) under IE 7, 8 and 9, Chrome and Firefox. It also runs fine on my public server under the same browsers. No errors are returned and the function does what it is supposed to do.

    The real head scratcher is that I uploaded a copy to a web server with a public address to have the client try. The client did not run into the JavaScript error (using the same browser and computer as before). Everything worked just like it was designed.

    In case of a corrupt file, I had the client overwrite the existing application with a fresh copy (PHP and JavaScript) on the internal server with the same files that worked for him on the public server. No luck.

    The client's IT department claims that no changes have been made to the server. Other JavaScript components (JQuery UI tabs and form validation) work just fine.

    Since the web server is internal and the company is concerned about security, the web server is not connected to the internet. I don't think that this function is using JQuery. Regardless, the JQuery library (jquery-1.4.2.min.js) is stored locally instead of referencing the JQuery server so there shouldn't be an issue with some kind of deprecated command that I was using... if it worked before, it should work now.

    I am stumped. I have tried everything that I can think of to isolate the problem. Does anyone have any idea why this would be happening?


    Here is my JavaScript function:

    Code:
    function showConfirm(target, current) {
    	var r=confirm("Are you sure you want to delete this item?  This action cannot be undone!");
    	if (r == true) {
    		location.replace(target)
    	}
    	else {
    		location.replace(current);
    	}
    }

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    One thing you can try is using window.location.replace
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SgtLegend: Thanks for the response. I will give that a shot.


    In general, another thing that I thought of is that the function is being invoked by the OnClick action on the link in the HTML. Could that be causing the problem? Is there an issue with that action not being well supported?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by bmeadowcroft View Post
    In general, another thing that I thought of is that the function is being invoked by the OnClick action on the link in the HTML. Could that be causing the problem? Is there an issue with that action not being well supported?
    There should be no issue there at all.

    Is line 210 one of the location lines?
    Can you put together a test page so that many more people here can try your code out in their own browser?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    Non-Member
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bmeadowcroft View Post
    In general, another thing that I thought of is that the function is being invoked by the OnClick action on the link in the HTML. Could that be causing the problem? Is there an issue with that action not being well supported?
    The link needs to have a "return false" at the end of the onclick code to stop the link's default href action

    Code:
    <a href="#" onclick="confirmDelete(params); return false">Delete this post</a>

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dantel View Post
    Code:
    <a href="#" onclick="confirmDelete(params); return false">Delete this post</a>
    Do you think that would fix this error that the OP is experiencing?

    Code:
    Message: Object expected
    Line: 210
    Last edited by paul_wilkins; Jan 17, 2011 at 17:51.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I put together a test version that everyone can access. It doesn't have full functionality like uploading pictures and the confirmation email but it still works. None of those features are associated with the JavaScript functionality.

    It is located at: www.structuredpixels.com/bb-sitepoint/BulletinBoard.php

    Since the notification email doesn't work, you can delete your post on the Administration page: www.structuredpixels.com/bb-sitepoint/Administration.php.

    The Administration page is where my client was having trouble with the JavaScript.


    pmw57 and dantel: You're saying that I have to have a "return false" after the onClick? Line 210 is one of the links with the onClick lines.

    If I used the confirmDelete function, it would have to be something like this, right?

    Code JavaScript:
    function confirmDelete(deleteURL) {
    if(confirm("Are you sure?") {
    window.location.replace = deleteURL;
    }
    }


    I would use the onClick formatting that you suggested above:
    <a href="#" onclick="confirmDelete(params); return false">Delete this post</a
    Is this method more widely supported by browsers than my original one?

    I'm willing to give this a shot. Thanks for the suggestions.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    I'm using IE with "display a notification about every script error" enabled. IE reports a scripting error in the menu code:

    Line: 329 Error: Invalid argument.
    Code:
    document.body.appendChild(dropul) //move main ULs to end of document
    That's because the dropul doesn't contain a valid element. You can solve that by enclosing that line and the rest of the code in the loop, in a condition that checks if dropul contains a valid element.

    Code javascript:
    if (dropul.nodeType === 1) {
        document.body.appendChild(dropul);
        ...
    }

    Firefox reports the same problem, when you open up its Error Console (in Tools menu)
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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