1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Rep Power

    Rewriting href tags when page loads

    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>

    /* loop through record set and build an announcement */
    <div class=”announcement”>
    /* Announcement 1 */
    <p>For more information on this subject <a href=”/file_path/file0001.doc”>click here</a>.</p>

    <div class=”announcement”>
    /* Announcement 2 */
    <p>For more information on this subject <a href=”/file_path/file0002.doc”>click here</a>.</p>

    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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Rep Power
    With jQuery, you can easily find all <a> within part of the document, and rewrite their hrefs:
    $(".announcements a").each(function() {
        var href = $(this).attr("href");
        if (href.match(/^\/file_path/) {
            $(this).attr("href", "file_open.php?aFile="+href);
    Code not tested, but hopefully you get the idea.

IMN logo majestic logo threadwatch logo seochat tools logo