iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-31-2009, 09:06 PM
Moderator
WebProWorld Moderator
 
Join Date: Oct 2003
Location: Alberta, Canada
Posts: 879
weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6
Default Rewrite match a url in a dhtml script

A Javascript question:

I'd like as a first option (scripted) to be able to open PDF and external links in a new window, and have as a fall back option to open a download dialog for PDF files.

The rewrite match is giving me some trouble, can anyone help, please?

This is what I'm working with so far:

Code:
Javascript: dhtml.js

// v0.2
// dependency: attr(), addEvent()
// x is id value of container
// all anchors go into array
function newWindow(x) {
 if(!document.getElementById || !document.getElementsByTagName) return;
 if(!document.getElementById(x)) return;
 dx=document.getElementById(x);
 an=dx.getElementsByTagName('a');
 for(var i=0,n=an.length,url;i<n;i++) {
/*
=> rewrite match an[i] => 'download.php?file=' + $file => '' + $file
*/
  str="window.open('"+an[i].href+"','newWindow','height=480,width=640,status=yes,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,menubar=yes');return false;";
  an[i].setAttributeNode(attr('onclick',str));
  an[i].setAttributeNode(attr('target','_blank'));
 };
};

// Attribute constructor
function attr(x,y){
 var z = document.createAttribute(x);
     z.nodeValue = y;
 return z;
};

/*
 echo "Required: 'window.load' event handler such as, "
 echo "addEvent(), "
 echo "by Dean Edwards."
*/

HTML Code:
<head>
.
.
<script type="text/javascript" src="/js/dhtml.js"></script>
</head>

<body>
.
.
.
<div id="extra">
<p class="pdf"><a href="download php?file=brochure.pdf" title="Printable .., PDF"><span>Brochure</span></a></p>
<p class="pdf"><a href="download php?file=calendar.pdf" title="Printable .., PDF"><span>Calendar</span></a></p>
<p class="pdf"><a href="download php?file=newsletter.pdf" title="Printable .., PDF"><span>Newsletter</span></a></p>
<p class="adobe"><a title="adobe.com" href="#example.com"><span>Get Adobe Reader</span></a></p>
</div>
.
.
.
<script type="text/javascript"><!--
addEvent(window,"load",function(){newWindow('extra')});
//--></script>
</body>
If scripts are running, the href will be just the file name, if not, the url will include the download.php page with file name in query string (default code).

I also plan to rewrite the title attributes to read "Open PDF in new window."

class="pdf" gets a PDF icon, and adobe a logo.

Any advice will be appreciated. Thanks.
Reply With Quote
  #2 (permalink)  
Old 08-31-2009, 11:08 PM
Moderator
WebProWorld Moderator
 
Join Date: Oct 2003
Location: Alberta, Canada
Posts: 879
weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6
Default Re: Rewrite match a url in a dhtml script

Well, I didn't give up and found my error, or at least another way to make a regular expression work.

Does this look clunky?

Code:
javascript


function newWindow(x) {
 if(!document.getElementById || !document.getElementsByTagName) return;
 if(!document.getElementById(x)) return;
 dx=document.getElementById(x);
 an=dx.getElementsByTagName('a');
 for(var i=0,n=an.length,url;i<n;i++) {
  url=filter(an[i].href);
  an[i].href=url;
  str="window.open('"+an[i].href+"','newWindow','height=480,width=640,status=yes,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,menubar=yes');return false;";
  an[i].setAttributeNode(attr('onclick',str));
  an[i].setAttributeNode(attr('target','_blank'));
  an[i].title="Open PDF in a new window";
 };
};

function filter(str){
 return str.replace(/download\.php\?file\=/, '');
}
Reply With Quote
  #3 (permalink)  
Old 09-01-2009, 07:04 PM
WebProWorld Member
 
Join Date: Jul 2006
Posts: 30
martyn-it RepRank 1
Default Re: Rewrite match a url in a dhtml script

Not clunky at all - It will do the job.

Only thing I would possibly change is instead of putting all the window open commands in every link, write a opener function that has all the code - (window.open('"+an[i].href+"','newWindow','height=480,width=640,status= yes,resizable=yes,scrollbars=yes,toolbar=yes,locat ion=yes,menubar=yes'))
in and pass the link to it

Martyn
__________________
Martyn
Martyn-it
Reply With Quote
  #4 (permalink)  
Old 09-01-2009, 10:38 PM
Moderator
WebProWorld Moderator
 
Join Date: Oct 2003
Location: Alberta, Canada
Posts: 879
weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6
Default Re: Rewrite match a url in a dhtml script

That was a great suggestion, martyn-it. Thank you! I gave it some thought and came up with this working version:

Code:
Javascript:


function windowOpen(x) {
 window.open(x,'newWindow','height=480,width=640,status=yes,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,menubar=yes');
}
function newWindow(x) {
 if(!document.getElementById || !document.getElementsByTagName) return;
 if(!document.getElementById(x)) return;
 dx=document.getElementById(x);
 an=dx.getElementsByTagName('a');
 for(var i=0,n=an.length,url;i<n;i++) {
  url=filter(an[i].href);
  str="windowOpen(\""+url+"\"); return false;";
  an[i].href=url;
  an[i].setAttributeNode(attr('onclick',str));
  an[i].setAttributeNode(attr('target','_blank'));
  an[i].title="Open PDF in a new window";
 };
};
function filter(str){
 return str.replace(/download\.php\?file\=/, '');
}
Should I use some form of constructor with an array of parameters for window.open() or is this form sufficient enough?
Reply With Quote
  #5 (permalink)  
Old 09-02-2009, 02:25 AM
WebProWorld Member
 
Join Date: Apr 2009
Posts: 42
Bernd RepRank 0
Default Re: Rewrite match a url in a dhtml script

For pdf-files you can try this in yout htaccess:
Code:
SetEnvIf Request_URI "\.pdf$" requested_pdf=pdf
Header add Content-Disposition "Attachment" env=requested_pdf
It shows the "open with/save" dialog.
__________________
Swedish Knäckebrottanz
Reply With Quote
  #6 (permalink)  
Old 09-02-2009, 04:31 AM
Moderator
WebProWorld Moderator
 
Join Date: Oct 2003
Location: Alberta, Canada
Posts: 879
weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6
Default Re: Rewrite match a url in a dhtml script

Quote:
Originally Posted by Bernd View Post
It shows the "open with/save" dialog.
That would be as opposed to download.php? What an excellent suggestion.

But how would I give as a first option, a new window?

Last edited by weegillis; 09-02-2009 at 04:34 AM. Reason: change of perspective
Reply With Quote
  #7 (permalink)  
Old 09-02-2009, 04:40 AM
Moderator
WebProWorld Moderator
 
Join Date: Oct 2003
Location: Alberta, Canada
Posts: 879
weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6
Default Re: Rewrite match a url in a dhtml script

Just to update, I've added a switch for PDF or external link title attribute assignment:

Code:
javascript:


function windowOpen(x) {
 window.open(x,'newWindow','height=480,width=640,status=yes,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,menubar=yes');
}
function filter(x){
 return x.replace(/download\.php\?file\=/, '');
}
function newWindow(x) {
 if(!document.getElementById || !document.getElementsByTagName) return;
 if(!document.getElementById(x)) return;
 dx=document.getElementById(x);
 an=dx.getElementsByTagName('a');
 for(var i=0,n=an.length,url,val;i<n;i++) {
  url = filter(an[i].href);
  val = "windowOpen(\"" + url + "\"); return false;";
  an[i].href = url;
  an[i].title = pdfTest(url);
  an[i].setAttributeNode(attr('onclick',val));
  an[i].setAttributeNode(attr('target','_blank'));
 };
};
function pdfTest(x) {
 var pdf = /\.pdf/;
 return (!pdf.test(x))?"Opens link in new window":"Opens PDF in a new window";
}
Being a learner, there is a standing order for more advice, so have at 'er!
Reply With Quote
  #8 (permalink)  
Old 09-02-2009, 06:49 AM
WebProWorld Member
 
Join Date: Apr 2009
Posts: 42
Bernd RepRank 0
Default Re: Rewrite match a url in a dhtml script

Quote:
Originally Posted by weegillis View Post
That would be as opposed to download.php? What an excellent suggestion.

But how would I give as a first option, a new window?
When the user selects open with ... normally his Acrobat Reader or a compatible programm is used. That's your new window for the pdf-files. The other choise from the dialog is to download - that's your fallback solution.
__________________
Swedish Knäckebrottanz
Reply With Quote
  #9 (permalink)  
Old 09-02-2009, 02:06 PM
Moderator
WebProWorld Moderator
 
Join Date: Oct 2003
Location: Alberta, Canada
Posts: 879
weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6
Default Re: Rewrite match a url in a dhtml script

The real issue for me is what the user is expecting. I've tried the dialog box method and didn't get universal support amongst my limited test group. They would all prefer the PDF to open in separate window on first click.

I say limited, but this is not something to be quickly discounted. It was unanimous. That's why I am more determined to have a new window automatically, and a download dialog by default.
Reply With Quote
  #10 (permalink)  
Old 09-02-2009, 08:05 PM
Moderator
WebProWorld Moderator
 
Join Date: Oct 2003
Location: Alberta, Canada
Posts: 879
weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6weegillis RepRank 6
Default Re: Rewrite match a url in a dhtml script

Additional update note:

The load event handler function can contain an array of fragment id's for multiple calls to newWindow(), rather than making multiple addEvent() calls.

Code:
Javascript
<body>
.
.
<script type="text/javascript"><!--
addEvent(window,"load",function(){
 f = new Array(
  'extra','jumpstart','external'
 );
 for ( var i = 0, n = f.length; i < n; i++ ) {
 newWindow( f[i] );
 };
})
//--></script>
</body>
This has been tested in new browsers only.

Last edited by weegillis; 09-02-2009 at 08:08 PM. Reason: Better wording
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
dhtml script delnaaz Search Engine Optimization Forum 0 02-21-2009 05:32 AM
Overture Account: Content Match Summary and Precision Match w3crawler Yahoo! Discussion Forum 0 03-04-2005 02:48 AM
DHTML HELP! Please help me Please! soulja90 Graphics & Design Discussion Forum 1 10-01-2004 08:36 AM
DHTML EFFECTS attitudefever Graphics & Design Discussion Forum 2 03-13-2004 02:30 AM
DHTML and IE for Mac question stevolane Web Programming Discussion Forum 3 11-26-2003 07:03 PM


All times are GMT -4. The time now is 12:06 PM.



Search Engine Optimization by vBSEO 3.3.0