SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple case AJAX alternative

    If I want to send something simple to the server and I don't need a response, instead of using AJAX, I use this:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function sendToServer(someText) {
    	var fakeImg = new Image();
    	fakeImg.src = "http://localhost/takeparam.asp?param=" + escape(someText);
    }
    </script>
    </head>
    <body>
    <input type="button" value="press me" onclick="sendToServer('baloney');" />
    </body>
    </html>
    Is there anything inherently "wrong" with this? I know that it requires javascript enabled. As far as I can tell it will work on more platforms that AJAX so that should be a good thing, right?

  2. #2
    I'm a splitzer's Avatar
    Join Date
    Dec 2005
    Location
    California
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didnt know, we could do this type of stuff with js. Since i never used it before.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,593
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Well that <input> tag needs to be in a form for the HTML to be valid.

    escape is deprecated so you should use encodeURIComponent instead.

    Apart from that it should work on all the same browsers that support Ajax as well as a few antique browsers that no one uses anymore.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a problem with some mozilla browsers when you do this. The status bar displays "Connecting with http://localhost .." and doesn't go away. This happens even if the actually object being returned is an image.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,593
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Well it would help if you set up the server side code so that it actually returns a single pixel transparent image. That way the browser wont get stuck trying to load the image.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall
    Well it would help if you set up the server side code so that it actually returns a single pixel transparent image. That way the browser wont get stuck trying to load the image.

    If it returns an image it'll just display a different message and sit there on that message. I can't quite remember what message it displays though although it wasnt acceptable where we were using it. On some apps this is probably ok. This problem is documented on the bugtracker over at Mozilla and may be fixed in ff 1.5 at least.

  7. #7
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is nothing wrong with this at all, but depending on the nature of what you're doing, you may run into caching problems (if the browser caches the image, the server never gets the request)

    Appending a unique value (counter or timestamp or such) to the query string will sort that out.
    Only dead fish go with the flow

  8. #8
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow !!!!!

    this is a nice way to have a counter sort of thing for a plain HTML page. also using the solution suggested by DEK can make sure that the hit will go to the server every time.
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.


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
  •