Link to home
Start Free TrialLog in
Avatar of andy7789
andy7789

asked on

jscript: how to assign onClick events with mootools

Hi,

I need removing href component for all links on the page and adding onClick function call for the same href link (slightly modified). Here is my code:

<script type="text/javascript">
<!--
function ajaxLinks () {
var target;
for (var i=0;i < document.links.length;i++) {
if(document.links[i].href.substr(0,<?php echo strlen($mosConfig_live_site)?>)=="<?php echo $mosConfig_live_site ?>"||document.links[i].href.substr(0,9)=="index.php:"){
target = 'index2.php'+document.links[i].href.substring(<?php echo strlen($mosConfig_live_site)+10?>);
alert(target);
document.links[i].addEvent('click', function(){xajax_multiply(target);mySpinner(); alert(target);});
document.links[i].href = "#";
}}}
ajaxLinks();
-->
</script>

I cannot understand what is happening, because

1) it gives me the correct alerts when the page is loading (ALL the links are different as it should be)
2) however it attaches only the LAST link on the page to ALL onClick events. When clicking on any of those links AFTER the jscript is run, it alerts the same (i.e. last) link

Please, help as it drives me crazy

Thank you!

A
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, remove the last || or part from my test condition.
Avatar of andy7789
andy7789

ASKER

Zvonko,

What can I say? you are the best!. It works perfectly, i only had to add "?" to the

target = 'index2.php?'+lnkPar[1];

A few questions to you:

1) what would be the best option to run this (a) before the </body> tag (b) run onload="ajaxLinks()" or (c) make it as window.addEvent('domready', function () {.....}

2) since I load the content (it is joomla's framework) via xajax, there is no http link for the page anymore and the system cannot  recognize what the page is (to load selected modules). Any ideas (even it sounds a bit crazy) how to combine it with the page link?
for 1) I would prefer option (a)

for 2) I did not understand the question. But if the question is: how to scan AJAX loaded links and change them too? is my answer: assign the loaded html text to a div.innerHTML and rescan the body links with the upper function again.

By the way, better choice then link rewriting and ajax loads and manipulation would be web server side scripting. But you know that already and have reasons for doing client side rewrites, right?