Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with Links--How to import filehandle into link?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am currently redesigning a large website. As part of the process, I have created printer friendly versions of every single web page. I would like to create a link on each formatted page to its printer friendly version using a script so that I do not have to do it manually on over a thousand different pages.

The printer friendly pages have the same name as their formatted versions, but are saved in a sub-directory called /ptr. I do not know how to automate this process so that the printer friendly link on each page automatically imports that page's filehandle.

For example, if the webpage was named:


the link createed by the script would link to


Does anyone know how to do this or have any ideas?

Thanks in advance for any tips.
 
Try something like:
Code:
<script>
document.write( &quot;<a href='ptr&quot; + location.pathname + &quot;'>Printer Friendly</a>&quot; );
</script>
Cheers, Neil
 
Thanks so much to Neil! I'm almost there now. Just one more problem - is there a way to extract just the filename? I also have &quot;ptr&quot; directories in my site's subdirectories, such as:

(formatted)
(printable)

The &quot;location.pathname&quot; operator gives me the complete filename, resulting in a printer link that looks like:

/next/level/ptr/next/level/home.html
 
One method using split and join:
Code:
tokens = location.href.split( &quot;/&quot; );
file   = tokens[ tokens.length - 1 ];
tokens[ tokens.length -1  ] = &quot;ptr&quot;;
tokens[ tokens.length ]     = file;
document.write( '<a href=&quot;' + tokens.join( &quot;/&quot; ) + '&quot;>TEST</a>' );
What this does is create an array of tokens where the href contained '/' characters. It then saves the filename stored in the last tokens slot, replaces this with the string &quot;ptr&quot;, and then appends the filename. The href is then reconstructed using join.
Some browsers may not support split and join though :-(
Cheers, Neil
 
Another way using regular expressions:
Code:
href = location.href.replace( /(\/[^\/]+$)/, &quot;/ptr$1&quot; );
document.write( '<a href=&quot;' + href + '&quot;>TEST</a>' );

The regular expression is:
Code:
\/      - literal '/'
[^\/]+  - one or more of any character except '/'
$       - anchored to end of string

The replace string is:
Code:
/ptr    - literal string
$1      - replace contents matched in ( ) of regular expression

This may be more browser friendly.... Cheers, Neil s-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top