Results 1 to 7 of 7

Thread: Need to issue HTTP Get without opening window

  1. #1
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need to issue HTTP Get without opening window

    I'm hoping this is simple. I need to be able to click a link and issue an HTTP Get (as if I were opening a new window) without actually opening a window. I don't need any feedback from the get.

    Thanks,

    Richard

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,991
    Thanks
    43
    Thanked 3,198 Times in 3,160 Posts
    Blog Entries
    12

    Default

    Could you be more specific? Like any specific URL? Any specific reason you need to do this?

    Anyways, you can issue a GET via AJAX. Nothing will open, you will get something back (response codes and if the request is successful, the data from the request), but you don't have to use any of that. The easiest way to send the request is with jQuery. If the page already uses jQuery, and you don't care about the specifics, just do:

    Code:
    <a href="#" onclick="jQuery.ajax({url: this.href}); return false;">Send a Get that Does Nothing</a>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Are you trying to send information to the server but not see the response?

    1. This is possible. But note that it's usually a good idea to still get the response if possible and display an error for the user if it fails. It would be a problem if important data isn't saved.

    2. There's a way around this (if and only if: 1) you're using GET; 2) you do not need any response) that will avoid AJAX and avoid any possible cross-domain or other security issues. You can use an image. It sounds odd, but it works well in basically any browser that allows any JS at all. What you will do is the following:
    i) Load something like "image.jpg?variable=value" into the page (you can load it into a hidden element if you want or even display the results to show the user it was successful).
    ii) On the server, use PHP (or another serverside language) to treat that image as a PHP script, where instead of just showing an image file, it actually runs PHP and takes the information from the GET request and stores it (or whatever you want).
    Further notes:
    --Due to security restrictions, you can't do much of anything in JS to confirm that it actually worked (although you can, I believe, perhaps detect if the image loads or does not load). You can't, for example, check the filesize of the image to see if it's a binary yes/no response. (I've looked into this as a way around cross-browser AJAX restrictions, and it's blocked on the JS-receiving end.)
    --In order to process an image as PHP, you have three options: i) use "image.php" as the URL and use PHP to send image-format headers along with image data; ii) do basically the same but also call it "image.jpg" and tell the server to treat ".jpg" as a PHP file (best for this file only) [the logic is exactly like working with .html files as PHP code, tutorials available online]; or iii) modification of ii: use .htaccess mod_rewrite to silently redirect the request and serve image.php AS the request for image.jpg.


    If that all sounds like way more than you want to deal with, feel free to ignore this post (though it may help others with a similar question). If you just want AJAX, then John's reply will be fine.

    I suppose one use for this could just be to say "I'm still here" rather than letting the system do an automatic logoff for inactivity.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the help guys. I have a home automation server that can respond to HTTP triggers. I don't have the ability to reprogram the server, but the ajax solution sounds like it would work.

    To be specific, the URL I want to send looks like this: http://myhost:9999/Test?1=50

    So would this be correct?

    <a href="#" onclick="jQuery.ajax({http://myhost:9999/Test?1=50: this.href}); return false;">Send a Get that Does Nothing</a>

    Thanks again.

  5. #5
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,521
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    I believe it should be
    Code:
     <a href="#" onclick="jQuery.ajax({url:'http://myhost:9999/Test?1=50'}); return false;">Send a Get that Does Nothing</a>

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,991
    Thanks
    43
    Thanked 3,198 Times in 3,160 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by keyboard1333 View Post
    I believe it should be
    Code:
     <a href="#" onclick="jQuery.ajax({url:'http://myhost:9999/Test?1=50'}); return false;">Send a Get that Does Nothing</a>
    That's right, I think. To be on the safe side specify the url and the data separately:

    Code:
    <a href="#" onclick="jQuery.ajax({url: 'http://myhost:9999/Test', data: '1=50'}); return false;">Send a 1 = 50 Get to myhost:9999/Test</a>
    And, as I was saying, you have to have jQuery already loaded on the page.

    Something else occurred to me though. Since you don't care if you get anything back, you could target a hidden frame or iframe with a normal link.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You guys are great. I'll try the hidden frame trick later today.

    Thanks,

    Richard

Similar Threads

  1. Opening a new window within a window with facebox.
    By maxisky in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 11-18-2008, 01:43 PM
  2. Opening a new window when closing another
    By Juniper79 in forum JavaScript
    Replies: 0
    Last Post: 10-12-2008, 04:32 AM
  3. DHTML Window Widget: Close Window-Layer(s) While Opening Another
    By twohawks in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 10-06-2007, 07:03 PM
  4. opening in a new window
    By eurows in forum JavaScript
    Replies: 4
    Last Post: 08-31-2007, 06:51 AM
  5. Auto center window script- Opening another window
    By gerlis in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 12-15-2006, 10:41 AM

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
  •