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
andy7789Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZvonkoSystems architectCommented:
Check this:
function ajaxLinks () {
  var target;
  var lnk = document.links;
  var liveSite = "<?php echo $mosConfig_live_site ?>";
  for (var i=0;i<lnk.length;i++){
    if(lnk[i].href.match(liveSite)||lnk[i].href.match("index.php")||lnk[i].href.match(".htm")){
      var lnkPar = lnk[i].href.split("?");
      if(!lnkPar[1]) lnkPar[1]="?";
      target = 'index2.php'+lnkPar[1];
      lnk[i].onclick = new Function("target='"+target+"';xajax_multiply(target);mySpinner(); alert(target);");
      lnk[i].href = "#";
    }
  }
}
ajaxLinks();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZvonkoSystems architectCommented:
Sorry, remove the last || or part from my test condition.
0
andy7789Author Commented:
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?
0
ZvonkoSystems architectCommented:
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?

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JScript

From novice to tech pro — start learning today.