sneaky
sneaky

Reputation: 2201

How to change src of application/pdf using javascript?

I have an embeded code like this

<embed id="1000" style="display: ;" width="850" height="1100" src="/new_folder/hi.pdf"       type="application/pdf">
</embed>

and i have this code

<a onfocus="OnLink(this)" href="javascript:switch('1000','/new_folder/bye.pdf');" onmousedown="return VerifyHref this,event,'{$XmlDefinition/List/@DefaultItemOpen}','{$thisNode/@HTML_x0020_File_x0020_Type.File_x00    20_Type.mapcon}','{$thisNode/@serverurl.progid}')" >
<xsl:value-of select="$thisNode/@FileLeafRef.Name" />
</a>  

and I have this function

<script type="text/javascript">
function switch(id,dirToPDF) { 
    $('1000').attr('data','dirToPDF'); 

} 
</script>

So when I click on the link, it should call switch, and change the pdf thats displayed (which is hi.pdf) to bye.pdf, but it's not working.

Can anyone help me with this?


EDIT:

Ok so I changed it to

<script type="text/javascript"> 
  function switchFunc(id, dirToPDF) {  
    $(id).attr('data', dirToPDF); 
  } 
</script> 


<a onfocus="OnLink(this)" href="javascript:switchFunc('pdf_d','/new_folder/bye.pdf');" onmousedown="return VerifyHref(this,event,'{$XmlDefinition/List/@DefaultItemOpen}','{$thisNode/@HTML_x0020_File_x0020_Type.File_x0020_Type.mapcon}','{$thisNode/@serverurl.progid}')" >
  <xsl:value-of select="$thisNode/@FileLeafRef.Name" />
</a>  


<embed id="pdf_d" style="display: ;" width="850" height="1100" src="/new_folder/hi.pdf" type="application/pdf">
</embed>

and it's still not working...

Upvotes: 1

Views: 1388

Answers (2)

SoreThumb
SoreThumb

Reputation: 530

Let me review:

  • As @user1113426 said, you call a custom-defined 'switch' function to switch attributes-- however, switch is a reserved function, and I don't think you can overwrite it.
  • As I said (then deleted) and @michaeldcooney said, you need to put a '#' sign before 1000; Turning $('1000') to $('#1000').
  • As @NickBeranek said, you can't have an id field be completely numeric-- so 1000 won't work anyways. (Try the name of the doc-- NewDocPdf)
  • Finally, the element attribute you are changing (data) is not the correct element to change. You want to change src, as that is the one that chooses what document is loaded.

Related: You have both an 'onmousedown' event AND an action when the link is clicked. They might cause interference, so it may be easier to do this..

The Doc Window:

<embed id="PDFDisplay" style="display: ;" width="850" height="1100" src="/new_folder/hi.pdf" type="application/pdf">
</embed>

Links to Change:

<a class="altDoc" href="javascript:return void;" whichDoc="/new_folder/bye.pdf" >
<xsl:value-of select="$thisNode/@FileLeafRef.Name" />
</a>  

Next:

<script type="text/javascript">

    switchFunc = function (jQueryObject) {
        console.log(jQueryObject);
        $('#PDFDisplay').attr('src',jQueryObject.attr('whichDoc') );
    }
    $('.altDoc').hover( function () { OnLink(this); } );
    $('.altDoc').click( function (event) {
        console.log("this");
        switchFunc(this);
        return VerifyHref(this,event,'{$XmlDefinition/List/@DefaultItemOpen}','{$thisNode/@HTML_x0020_File_x0020_Type.File_x00 20_Type.mapcon}','{$thisNode/@serverurl.progid}');
    } );
</script>

Let me know how the above works and I can provide any other alterations.

EDIT: Looks like I forgot a few )s, ;s, etc. Also, I kept a bad " and forgot an opening parenthesis after VerifyHref.

EDIT THE SECOND:

You ever consider writing a bunch of tags which have 'target="iFrameNameAttribute"' ? You can have links specifically target an iframe, and you can have the iFrame content set to the embedded PDF. Whenever you click on a link that targets the iFrame, it can change the content.

Upvotes: 1

Nick Beranek
Nick Beranek

Reputation: 2761

Please modify your script as follows and pass in the id parameter:

<script type="text/javascript">
  function switchFunc(id, dirToPDF) { 
    $('#' + id).attr('data', dirToPDF);
  }
</script>

switch is a reserved name in JS. Also, 1000 is not a valid id. ID's cannot start with a number.

Upvotes: 0

Related Questions