Solved

jscript: how to assign onClick events with mootools

Posted on 2007-11-25
4
5,906 Views
Last Modified: 2008-02-01
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
Comment
Question by:andy7789
  • 3
4 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 20348636
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 20348641
Sorry, remove the last || or part from my test condition.
0
 

Author Comment

by:andy7789
ID: 20348753
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 20353944
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question