www.webdeveloper.com
Results 1 to 9 of 9

Thread: escape apostrophy in URL

  1. #1
    Join Date
    Jul 2011
    Posts
    6

    Question escape apostrophy in URL

    Hereís the scenario:

    I have an announcement webpart on my sharepoint site. The boss wanted attachments to open in a new window so that users donít close the attachment and get knocked from the site only to have to login again. To achieve this we settled on a policy that attachments should be in the form of PDF documents rather than word format. We contacted Microsoft who in turn developed a solution for us that consisted of a content editor web part that runs some javascript. The problem is 2 fold.

    1. The javascript only works in Internet explorer. When used in other browsers the attachments open in the same window.

    2. If the name of the document contains an apostrophe, IE barks a javascript error: expected ď)Ē

    How do I rewrite the code to escape the apostrophe and to function in multiple browsers?

    Michael

    Code:

    <script language="javascript" type="text/javascript">
    _spBodyOnLoadFunctionNames.push("OpenPDFInNewWindow()");

    function OpenPDFInNewWindow()
    {
    // Get the collection of <a> tags
    var aAllLinks = document.getElementsByTagName('a');

    // For each <a> tag
    for(var i=0;i<aAllLinks.length;i++)
    {
    var oA = aAllLinks[i];
    var sHREF = oA.attributes["href"].value.toLowerCase();

    // Is this a pdf link?
    if(sHREF.indexOf(".pdf") > 0)
    oA.href="javascript:window.open('" + sHREF + "').focus();";
    }
    }
    </script>

  2. #2
    Join Date
    Dec 2008
    Posts
    488
    first of all, theres a quotation mark and the end of your last statement followed by a semi-colon.

    anyway, you'll need to add the escape function

    Code:
    var sHREF = escape(oA.attributes["href"].value.toLowerCase());

  3. #3
    Join Date
    Jul 2011
    Posts
    6

    Thumbs down error not fixed.

    I added the escape code as you suggested. I then tried uploading a document with an apostrophe and got the same error. when I look at the address I get:

    javascript:window.open('http%3A//rcda-portal/lists/parish%2520announcments/attachments/56/peter%27s.pdf').focus();

    I can see that the apostrophe was replaces with %27 but I still get the error: expected ")"

  4. #4
    Join Date
    Jul 2011
    Posts
    6
    oA.href="javascript:window.open('" + sHREF + "').focus();";

    also wanted to mention that the last " is there to end the string.
    the ; is there to end the line

  5. #5
    Join Date
    Jul 2011
    Posts
    6
    the other thing I noticed is that firefox does not like the script and does not update the links to include the window.open code.

  6. #6
    Join Date
    Dec 2008
    Posts
    488
    Oh, I'm sorry, I wasn't paying enough attention. I should have looked more closely at what you were doing there. You don't want to change the HREF of a link, you simply want to add an event to these links. I was also wondering why you don't just give the links target="_blank"... (edit>> Nevermind, I see now, just went and looked at how the attachments are done).

    Anyway, rather than changing the HREF of the link, here's how we'll add an onclick event to the links, instead:

    Code:
    <script type="text/javascript">
    _spBodyOnLoadFunctionNames.push("OpenPDFInNewWindow()");
    
    function addEvent(obj, evt, fnc, bubble) {
        //A cross-browser event attachment function
        if (!bubble) {
            bubble=false;
        }
        if (obj.addEventListener) {
            obj.addEventListener(evt, fnc, bubble);
            return true;
        }
        if (obj.attachEvent) {
            return obj.attachEvent("on" + evt, fnc);
        }
    }
    
    
    function OpenPDFInNewWindow() {
        // Get the collection of <a> tags
        var aAllLinks = document.getElementsByTagName('a'), i;
    
        // For each <a> tag
        for (i = 0; i < aAllLinks.length; i++) {
            // Is this a pdf link?
            if(aAllLinks[i].href.toLowerCase().indexOf(".pdf") > 0) {
                addEvent(aAllLinks[i], "click", function () {
                    window.open(this.href).focus();
                    return false;
                }, false);
            }
        }
    }
    
    </script>
    This way, if javascript is off, or there is an unexpected error, the links will still work. Also, you won't have to worry about escaping apostrophes using this method.
    Last edited by jamesbcox1980; 07-14-2011 at 04:38 PM.

  7. #7
    Join Date
    Jul 2011
    Posts
    6
    Thanks for pointing me in the right direction.

    I tried swapping your code for the original script I had and I do get a popup window. There is however a problem. The window displays ď404 not foundĒ and the url is http://rcda-portal/Lists/Parish%20An...ents/undefined

    I thinking it has to do with the value of this.href, but Iím not sure what.

    Iím going to monkey with it and see what happens. If you have any idea, please share.

  8. #8
    Join Date
    Jul 2011
    Posts
    6

    Solved!

    It works!

    Instead of adding the listener I simply added target="_blank" to the pdf links. It's now cross browser compliant...as far as IE and firefox.

    Thanks again. I owe you one.

    Mike

    <script type="text/javascript">
    _spBodyOnLoadFunctionNames.push("OpenPDFInNewWindow()");


    function OpenPDFInNewWindow()
    {
    // Get the collection of <a> tags
    var aAllLinks = document.getElementsByTagName('a'), i;

    // For each <a> tag
    for (i = 0; i < aAllLinks.length; i++)
    {
    // Is this a pdf link?
    if(aAllLinks[i].href.toLowerCase().indexOf(".pdf") > 0)
    {
    aAllLinks[i].target="_blank";
    }
    }
    }

    </script>

  9. #9
    Join Date
    Dec 2008
    Posts
    488
    I think it must be how your particular code is dealing with the "this" keyword. Try changing "this.href" to "aAllLinks[i].href". I'm guessing it's because we're feeding an event handler into another event handler. When I did it with window.onload instead of your page's onload handler it works fine. That's the only reason I can see that it might not be working.

    I prefer the "target = '_blank'" method over a popup any day, just because modern browsers don't know when you're just doing a pop up and when you have a legitimate reason to open a popup window.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles