Results 1 to 5 of 5

Thread: How to Open PDF with JavaScript?

  1. #1
    Join Date
    Jul 2006
    Posts
    218

    How to Open PDF with JavaScript?

    I am very new to the JavaScript. I have given an assignment for opening the PDF. Since, I know very less about the JavaScript, even after trying for lot times I am unable to get the correct result. I want to open the PDF file using a standalone Javascript file. Can anyone tell me how to open PDF with JavaScript.?? Any other information related to the topic is warmly welcomed. Since, I am new please tell me the solutions in simple words.
    ~*~Silent~Kid~*~
    "To The World You May Be Just One Person, But To One Person You May Be The World"

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: How to Open PDF with JavaScript?

    Even I agree that it would be useful if you can open the PDF using the JavaScript. Instead of opening that PDF manually, it would be better to open that automatically. In simple I would like to say that opening documents such as PDFs in a new window should be automated using JavaScript because Opening a new window is a behavior and should be moved to the behavioral layer. You can use the JavaScript, because it is more useful when a website is content managed. You can open the PDF from JavaScript,rather than having to rely on site editors to remember to open a link to a PDF in a new window. This whole process can be handled by a simple JavaScript function.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to Open PDF with JavaScript?

    If you want to open the PDF using the JavaScript, then you would have to create an event first. This event should occur when the page is loaded. I have provided you with the sample of the codes which would be helpful for you. The code you will have to type into the .js file. Here is the coding :
    Code:
    function addLoadEvent(func)
    {
    var onload1 = window.onload;
    if (typeof window.onload != 'function')
     { 
    window.onload = func;
     } 
    else
     { window.onload = function() 
         { 
           onload1(); func(); 
         } 
     } 
    }
    addLoadEvent(fNewPDFWindows);

  4. #4
    Join Date
    Mar 2008
    Posts
    672

    Re: How to Open PDF with JavaScript?

    It would be better if you create an empty function in your JavaScript file. I would recommend you to do the same. So create an empty function in your JavaScript file called fNewPDFWindows. Also first check to see if the command getElementsByTagName is available. By doing this you will be assured that there is no JavaScript errors with the script.
    Code:
    function fNewPDFWindows () 
    { 
    if (!document.getElementsByTagName) return false; 
    }
    After doing the first step, you will have to create an HTML object collection. Now this object can be of any links but must be within the page. For doing the same, you can write down the following code :
    Code:
    var links = document.getElementsByTagName("a");

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to Open PDF with JavaScript?

    When I looked at the code mentioned by the 'Allan.d', I thought that if you do the looping that would be much better. So try to do that which can be more useful. First create the loop that goes through all the links in the page. You can do this by writing the code given down :

    Code:
    function fNewPDFWindows () 
    { 
      if (!document.getElementsByTagName) return false; 
      var links = document.getElementsByTagName("a"); 
      for (var eleLink=0; eleLink < links.length; eleLink ++) { } 
    }
    Hope that this helps you.!!

Similar Threads

  1. Need JavaScript Code to open URL
    By AM-Anthony in forum Software Development
    Replies: 2
    Last Post: 12-04-2012, 06:50 PM
  2. Replies: 4
    Last Post: 23-08-2011, 10:19 PM
  3. JavaScript - open link in same window using location.replace
    By Elizabeth Allen in forum Software Development
    Replies: 7
    Last Post: 13-05-2010, 12:08 PM
  4. How to Open Popup Window in Javascript?
    By Soumen in forum Software Development
    Replies: 4
    Last Post: 07-02-2010, 04:02 AM
  5. Can't open Hotmail msgs, "javascript;" @ lower left
    By Klangpup in forum Vista Help
    Replies: 3
    Last Post: 06-07-2008, 07:19 AM

Tags for this Thread

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
  •  
Page generated in 1,713,272,845.42990 seconds with 17 queries