Solved

jscript: how to assign onClick events with mootools

Posted on 2007-11-25
4
5,908 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery Syntax... 4 50
JS library vs Js framework 11 21
Video Tutorial help 2 37
Can anyone help me find this preloader? 51 39
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

763 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