jostclan Posted September 9, 2013 Share Posted September 9, 2013 (edited) 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 September 9, 2013 by jostclan Quote Link to comment https://forums.phpfreaks.com/topic/282017-rewrite-a-href-tags-on-load/ Share on other sites More sharing options...
kicken Posted September 9, 2013 Share Posted September 9, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/282017-rewrite-a-href-tags-on-load/#findComment-1448869 Share on other sites More sharing options...
jostclan Posted September 9, 2013 Author Share Posted September 9, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282017-rewrite-a-href-tags-on-load/#findComment-1448873 Share on other sites More sharing options...
Irate Posted September 9, 2013 Share Posted September 9, 2013 Can you use jQuery on your site? Quote Link to comment https://forums.phpfreaks.com/topic/282017-rewrite-a-href-tags-on-load/#findComment-1448874 Share on other sites More sharing options...
jostclan Posted September 9, 2013 Author Share Posted September 9, 2013 I can certainly use JQuery, but I do not know the language... do you have some code which would help resolve this? Quote Link to comment https://forums.phpfreaks.com/topic/282017-rewrite-a-href-tags-on-load/#findComment-1448876 Share on other sites More sharing options...
Irate Posted September 9, 2013 Share Posted September 9, 2013 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); } }); Quote Link to comment https://forums.phpfreaks.com/topic/282017-rewrite-a-href-tags-on-load/#findComment-1448878 Share on other sites More sharing options...
DavidAM Posted September 9, 2013 Share Posted September 9, 2013 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']);. Quote Link to comment https://forums.phpfreaks.com/topic/282017-rewrite-a-href-tags-on-load/#findComment-1448894 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.