Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1118
  • Last Modified:

jscript: adding onClick events for the links

Hi,

I need to add onClick events and remove href values for All links in the document. here is a simple code:

<script type="text/javascript">
<!--
function ajaxLinks () {
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:"){
//document.links[i].href = "javascript:xajax_multiply('index2.php"+document.links[i].href.substring(<?php echo strlen($mosConfig_live_site)+10?>)+"')";
document.links[i].onclick = "xajax_multiply('index2.php"+document.links[i].href.substring(<?php echo strlen($mosConfig_live_site)+10?>)+"')";
document.links[i].href = "#";
}}}
ajaxLinks();
-->
</script>
The problem is that id does not add the onClick event, but only makes href="#"

The first command (commented), i.e.

//document.links[i].href = "javascript:xajax_multiplle......

works fine.

What is wrong here?

Pleas, help
0
andy7789
Asked:
andy7789
  • 10
  • 9
  • 2
1 Solution
 
gops1Commented:
do it this way:

document.links[i].onclick = function(){
                 "xajax_multiply('index2.php"+document.links[i].href.substring(<?php echo strlen($mosConfig_live_site)+10?>)+"')";
}
0
 
andy7789Author Commented:
still it returns a mess like

index2.php"+document.links[i].href.substring(34)+"

as an argument - we should check all "" and ' here...
0
 
andy7789Author Commented:
I am still getting troubles with this... this is the final code:

<script type="text/javascript">
<!--
function ajaxLinks () {
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:"){
var target = 'index2.php'+document.links[i].href.substring(<?php echo strlen($mosConfig_live_site)+10?>);
document.links[i].onclick = xajax_multiply(target);
document.links[i].href = "#";
}}}
ajaxLinks();
-->
</script>

INstead of adding onClick event to the link, it keeps calling function xajax_multiply in cycle. When the cycle is over, it does not add onClick event anyway

Any ideas?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
gops1Commented:
call ajaxLinks() onload of body.

window.onload=function(){
           ajaxLinks();
}
0
 
andy7789Author Commented:
it looks as i cannot attach a new event to the link element. Since I am using a mootools library, I have added it like

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?>);
//document.links[i].onclick = xajax_multiply(target);
document.links[i].addEvent('click', function(){xajax_multiply(target)});
document.links[i].href = "#";
}}}

It works, BUT adds ONLY the 1st link element, i.e. ALL the alaments have exactly the same xajax_multiply argument.

I just cannot figure out what is wrong with the cycle

Pls, have a look, if you spot something here
0
 
gops1Commented:
I have a little knowledge regarding php
it is got to do with your php logic. Once the php is rendered, this value <?php echo strlen($mosConfig_live_site)+10?>, it will be the same through out.  It will pickup only the last value.


0
 
andy7789Author Commented:
php is fine here, because it takes a substring, i.e.

document.links[i].href.substring(value)

anyway, when I make alert(target), it returns all correct and different outputs, but does not assign correct onClick values.
0
 
andy7789Author Commented:
It always adds the LAST link value...
0
 
gops1Commented:
This is what I am trying to tell you. Whatever is the last value that is going to be added up to all links. You need to loop them seperately
0
 
andy7789Author Commented:
Look, it is not like this (even it returns the correct alert values (target var).

The idea of the whole logic is as follows

1) document.links[].href returns the values like http://www.mydomain.com/index.php?com_var etc etc etc
2) I have to include onClick arguments like index2.php?com_var - the same string
3) php function finds the length of http://www...com, next adds the length of index.php and extracts it from the sgtring. Next the index2.php is added to the beginning of the string

It works perfectly when I alert all the target values, but does not add them to the onClick event
0
 
gops1Commented:
did you try replacing this line:

document.links[i].addEvent('click', function(){xajax_multiply(target)});

with this:

document.links[i].onclick=function(){
        xajax_multiply(target);
}
0
 
andy7789Author Commented:
yes i did - exactly the same. I even added an alert of the same target like

document.links[i].onclick=function(){
        xajax_multiply(target);
            mySpinner();
            alert(target);
}

Clicking on the link triggers the same alert of the same (last!) link
0
 
gops1Commented:
Then check for "if" condition is getting satisfied or not
0
 
gops1Commented:
ok try it this way:
function ajaxLinks () {
 
	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:"){
			document.links[i].onclick=function(){
				var target='index2.php'+this.href.substring(<?php echo strlen($mosConfig_live_site)+10?>);
				xajax_multiply(target);
				mySpinner();
				alert(target);
			}
			document.links[i].href = "#";
		}
	}
}

Open in new window

0
 
andy7789Author Commented:
what's the difference? it looks as my code. You only placed Var target inside
0
 
gops1Commented:
See before target was a global variable, so if once target gets the value then throughout the program the target will have the same value. Now if you place it inside at least we can be sure of that target gets a new value always else nothing. The other thing I have change is usage of this.href that us because according to you href is aways assigned the correct values
0
 
Michel PlungjanIT ExpertCommented:
What is xajax_multiply going to do with this code???

xajax_multiply('index2.php"+document.links[i].href.substring(<?php echo strlen($mosConfig_live_site)+10?>)+"')

0
 
gops1Commented:
I guess Andy has open another question with the same issue.

http://www.experts-exchange.com/Programming/Languages/Scripting/JScript/Q_22981762.html

Probably you can close this one.
0
 
andy7789Author Commented:
mplungjan,

this is a generated xjax function (see http://xajaxproject.org)

Yes, i have opened another question and the zvonko's solution works fine:


Still, i cannot understand, why adding

document.links[i].href = .....some function with i as an argument works fine, but

document.links[i].onClick = some function - does not work.
<script type="text/javascript">
<!--
function ajaxLinks () {
  var target;
  var lnk = document.links;
  //if(lnk[i].href.match(liveSite)||lnk[i].href.match("index.php")||lnk[i].href.match(".htm"))
  var liveSite = "<?php echo $mosConfig_live_site ?>";
  for (var i=0;i<lnk.length;i++){
    if(lnk[i].href.match("option=com_content")||lnk[i].href.match("option=com_contact")||lnk[i].href.match("option=com_alphacontent")||lnk[i].href.match("option=com_joomap")){
      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();");
      lnk[i].href = "#";
    }
  }
}
ajaxLinks();
 
-->
</script>

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
href is a string
onclick is an event handler and  needs a function
0
 
andy7789Author Commented:
yes, it is quite clear, but the way i tried it did no work:

document.links[i].addEvent('click', function(){xajax_multiply(target)}); // via mootools

or

document.links[i].onclick=function(){
        xajax_multiply(target);
}

both of those codes do not work
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 9
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now