Forum Moderators: open

Message Too Old, No Replies

Script to enter name of current file in a link

script file name

         

richiwatts

3:08 pm on Aug 21, 2005 (gmt 0)

10+ Year Member



I have over 150 pages where I need to have a link to their printer friendly version.

All of the printer friendly version pages have exactly the same name as the original with the word print_ before them.

e.g
contact.asp
print_contact.asp

I was wondering if there was a way that I could copy the same code into every file without having to keep changing the file name in the code.

Here is the code:
<a href="print_contact.asp" target="_blank">Printer friendly version</a>

Does anyone know how I can have a bit of code so that it gets the name of the current page. Something like

<a href="print_<current page name>.asp

Or something like that.

You would be saving me hours of work.

Thanks
Rich

JAB Creations

4:23 pm on Aug 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know EXACTLY how to do this but I'll show you a couple things I do know at least...

A little Javascript...

document.writeln(document.URL);

This will print out the FULL url of the page in Javascript such as "http://example.com/fishes.php".

A little PHP...

echo basename($_SERVER['PHP_SELF']);

This will JUST print out the filename itself, such as "example.php".

You probally need a string replace, I'll try to play around with it but if someone knows how to do this without havnig to bend over backwards go for it.

richiwatts

4:40 pm on Aug 21, 2005 (gmt 0)

10+ Year Member



Hi,

Thanks, but I am not worried about the printing part.

I am trying to open the printer friendly version in a new window.

I already have a printer freindly version for every page and the printer friendly version always has the same name as the page it is printing with print_ at the front.

So if my page is called About_us.asp
the printer freindly version is called print_About_us.asp.

I am trying to enter a bit of code that will somehow be able to get this name print_About_us.asp automatically. This will then svae me from haveing to change the link on every page and I can just search and replace all the files to enter the link.

Rich

JAB Creations

5:12 pm on Aug 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's going to require Javascript string replacement which I can only understand faintly right now. The target attribute should work fine...I use a script to do something somewhat similer on my site when someone breaks out of frames but I add the url to the end of a string and so I'm not using string replacement.

richiwatts

6:01 pm on Aug 21, 2005 (gmt 0)

10+ Year Member



I now have the answer. If anyone else needs it:

<a href="#" onclick="this.href = 'print_' + location.href.substr(location.href.lastIndexOf('/') + 1);">Print Friendly</a>

Longhaired Genius

6:16 pm on Aug 21, 2005 (gmt 0)

10+ Year Member



It would be best to do this serverside, in my opinion. It would be trivial to do in PHP and is probably also simple in ASP (but I am not familiar with ASP). Try the ASP forum tomorrow when there are more eyeballs on the board.