SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Function to open word document

    Hi all

    I created a page which has a button (template).
    When user clicks on it it should select one word document randomly from list of word documents.

    Can anybody tell me how to write javascript function for this.

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    I believe the document.open() file will open the Word document, but that is assuming that the users computer has a program that can handle word documents (if they don't, or they are on a browser like Firefox, they will get an "open or save" dialog).

    As for picking one at random, I recommend storing the word documents in an array. Then, use the Math.rand() function to randomly pick one. To achieve this, use num = Math.rand() * n + 1 where n is the number of documents you have in the array.

    The function could look something like this:
    Code:
    function randomDoc(docs) {
        var num = Math.rand() * docs.length + 1;
        document.open(docs[num]);
    }
    Hope that helps.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi srikanthn

    document.open will open it in a new window. however, you could just link to the file and change the links href, title and firstNode.textNode (innerHTML) as required.


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
  •