0

I am looking for a solution to the following scenario:

I'd like to write a script that opens a new window with a specified link (ex: http://yadayada.org/customer#.pdf). With the same function (or click) I'd like to to also open http://yadayada.org/customer#A.pdf if it exists. Below I have a working sample of a function that works to open the files in 2 new windows. However, I obviously get a blank window if the second location doesn't exist. I'd like the second page to not be opened if the reference does not exist.

function OpenFile() {
    win=window.open('http://yadayada.org/~(student_number).pdf', '_self');
    win2=window.open('http:/yadayada.org/~(student_number)a.pdf', '_blank');
    win.focus();
}

OpenFile();
Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
  • What do you mean 'if the second location doesn't exist'? If there's no page at the URL, or if there's no URL? And what HTML are you using, where's the second (or the *first* for that matter) URL/`href` coming from? – David Thomas May 20 '13 at 21:31
  • If there is no page at the URL. The PDF's are saved according to customernumber and sometimes there is an amendment saved (append an a to the customernumber in the file name-->customernumberA.pdf. So, if they don't have an amendment, 'http:/yadayada.org/~(student_number)a.pdf' will return a "object not found" window. – user1522051 May 20 '13 at 21:34
  • Sorry, I didn't see the second part of your question. One Page1, an image exists next to a customer based on certain conditions. If the icon is displayed and you click on it calls an – user1522051 May 20 '13 at 22:00

1 Answers1

0

You will need to check the http status code of the URL using JavaScript to determine if the page exists, and if so then open the link.

If you are using jQuery then it makes life easier: Use javascript to check http status codes

Otherwise you can use plain old JavaScript: How to get HTTP status from JavaScript

[Edit] As mentioned in the comments, this will not work cross-domain.

You could try the following - force the request to be JSONP by adding the ?callback=? parameter to the request, but you need to look at the .fail callback since the pdf is obviously not JSON data. Also, be aware that the entire PDF is downloaded, so there will be a delay for large files.

var url = "http://samplepdf.com/sample.pdf?jsoncallback=?";
$.getJSON(url)
    .fail(function( jqxhr, textStatus, error ) {
        if (jqxhr.status == 200) {
            console.log("request document found success");
        }
    })

"Working" Demo: http://jsfiddle.net/WmHjs/

I would try and make the call server side if you require cross-domain checking.

Community
  • 1
  • 1
Andreas Josas
  • 242
  • 2
  • 7
  • Thank you Andreas. Do you know if this still works if I am checking for a file on a different server? (cross-domain scripting) – user1522051 May 20 '13 at 22:12
  • No, due to the [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy). Is the JavaScript written out dynamically from serverside code? If so then you could test status code from server code and write out the javascript code. – Andreas Josas May 20 '13 at 22:49