• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5927
  • Last Modified:

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
0
andy7789
Asked:
andy7789
  • 3
1 Solution
 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now