Jump to content


Photo

'Rewrite a Href tags on load

href rewrite

  • Please log in to reply
6 replies to this topic

#1 jostclan

jostclan

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 09 September 2013 - 11:07 AM

I really need some help with this one.  I am confident it can be done easily, but i can not figure it out.

 

A very simplified look at my "announcements_current.php" page follows:

 

<div class=”main_nav”>

                <a href=”about.php”>About</a>

                <a href=”contact.php”>Contact</a>

                <a href=”resources.php”>Resources</a>

</div>

<div class=”announcement”>

     /* Announcement 1 */

     <p>For more information on this subject <a href=”/file_path/file0001.doc”>click here</a>.</p>

</div>

<div class=”announcement”>

     /* Announcement 2 */

     <p>For more information on this subject <a href=”/file_path/file0002.doc”>click here</a>.</p>

</div>

 

As the above page (announcements_current.php) loads it reads the mySQL database for current announcements, loops through each record and displays the page.  I would like to have all hyperlinks within the <div class="announcements"> rewritten on load to be <a href="file_open.php?aFile=/file_path/file0001.php">  (of course whatever the file name actually is).

My file_open.php  updates a database table with the date/time/user_id of when this file was accessed, then opens the file. 

 

Of course I do not want to rewrite any other hyperlinks not in the correct class.  I am sure this can be done, but i can not fire out how to accomplish this onload.  Can you help?


Edited by jostclan, 09 September 2013 - 11:09 AM.


#2 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,731 posts
  • LocationBonita, FL

Posted 09 September 2013 - 11:15 AM

It could be done sure, but why not just modify announcements_current.php to generate the proper links in the first place rather than apply some JS hack?
Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#3 jostclan

jostclan

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 09 September 2013 - 11:30 AM

kicken- These announcements are being created (added to the database) by staff using tinyMCE.  They select the insert hyperlink and pate the file file path.  They are not skilled enough, nor care enough to add the redirect.  I hope this helps answer the why.



#4 Irate

Irate

    Advanced Member

  • Members
  • PipPipPip
  • 358 posts
  • LocationHamburg, Germany
  • Age:17

Posted 09 September 2013 - 11:34 AM

Can you use jQuery on your site?
Quod placet mihi non placeat tibi. - What I think to be good must not always equal your perception of it.

I am not perfect. I try a lot with the code I provide and I don't guarantee for it to work as I have mostly no option to test it on my mobile phone. I do apologize for any inconvenience I caused, but if I do happen to have helped, liking my posts or marking them as to have solved or answered your question would be nice.

#5 jostclan

jostclan

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 09 September 2013 - 11:40 AM

I can certainly use JQuery, but I do not know the language... do you have some code which would help resolve this?



#6 Irate

Irate

    Advanced Member

  • Members
  • PipPipPip
  • 358 posts
  • LocationHamburg, Germany
  • Age:17

Posted 09 September 2013 - 11:54 AM

Okay, consider this, then.

$(function(){
var links = $('a[href^="/file_path/"]').get(), i = 0;
for( i; i < links.length; i++ ) {
var h = links[i], var t = h.getAttribute("href").replace("/file_path/","/file_open?aFile=");
h.setAttribute("href",t);
}
});

Quod placet mihi non placeat tibi. - What I think to be good must not always equal your perception of it.

I am not perfect. I try a lot with the code I provide and I don't guarantee for it to work as I have mostly no option to test it on my mobile phone. I do apologize for any inconvenience I caused, but if I do happen to have helped, liking my posts or marking them as to have solved or answered your question would be nice.

#7 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,976 posts
  • LocationSpring, TX USA

Posted 09 September 2013 - 02:08 PM

kicken- These announcements are being created (added to the database) by staff using tinyMCE.  They select the insert hyperlink and pate the file file path.  They are not skilled enough, nor care enough to add the redirect.  I hope this helps answer the why.


kicken's suggestion was to have announcements_current.php generate the links the way you want them while building the page in the first place. So instead of echo $row['TextFromDB']; you would write a function to convert the links then echo ConvertLinks($row['TextFromDB']);.
-- I haven't lost my mind, it's backed up on tape ... somewhere!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com