Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 Confirm fallow link

View previous topic View next topic Go down 
AuthorMessage
Tenizen
Member
Member
Tenizen

Posts : 3
Join date : 2011-04-18

Confirm fallow link Empty
PostSubject: Confirm fallow link   Confirm fallow link EmptyWed May 11, 2011 9:55 pm

Hello programmer and coders!

I´m just starting to learn about JavaScript. And Im trying to make a function were I can put all links that schould have an alert box coming up when they click the link. I have putted this function togheter (that dont work..yet!):
Code:

function init(){ // load the page

function go_to() {
   
        var link = new Array();

        //links that schould have this function:
        link[0] = "http://www.capoeiras.se/";
        link[1] = "http://www.koanuka.com";
        link[2] = "http://www.matochhushallning.blogspot.com";

        //ok or cancel:
        new_url = link[num];
        if (!confirm ("You are leaving this site, do you want that?")){
                window.location = new_url;
        } else {
         return false
 
        }
}



//här avslutas init
   
}
            

}
window.onload = init;
Do I need to have onclick here? Im thankful for any help I can get!!!
Back to top Go down
Tenizen
Member
Member
Tenizen

Posts : 3
Join date : 2011-04-18

Confirm fallow link Empty
PostSubject: Re: Confirm fallow link   Confirm fallow link EmptyWed May 11, 2011 9:56 pm

I changed my cod to this:
Code:

function init(){ // loading page

document.getElementsByTagName("a").onclick = goTo;

function goTo() {
   var links=document.getElementsByTagName("a");
        if (links.className=="extern")   
 
         if (!confirm ("You are leaving this site. Do you want to follow the link?")){
              
         return false;
 
        }
      return true;
}

                  



//här avslutas init
   
}
            

}
window.onload = init;
//slut på init
I was thinking to make a simple cod like this (but working!). I have put the name "extern" on every link that I want this confirm box on. Can my code work? And if what do I need to ad/change?
Back to top Go down
Knowledge
Member
Member
Knowledge

Posts : 21
Join date : 2011-03-09

Confirm fallow link Empty
PostSubject: Re: Confirm fallow link   Confirm fallow link EmptyWed May 11, 2011 9:57 pm

The getElementsByTagName function returns an array of elements so you have loop through the array and assign the onclick handler to each anchor. Since you've already coded the extern class on all the links, you could apply the onclick handler only to those anchors with that class while going through the loop. Then you don't need any code in the goTo function to determine whether the link is external.

Please examine the code below and let me know if you have any questions.
Code:

function goTo(){
   return confirm("You are leaving this site. Do you want to follow the link?");
}

function init(){
   var anchors = document.getElementsByTagName("a");
   for(var i = 0; i < anchors.length; i++){
      if(anchors[i].className == "extern")
         anchors[i].onclick = goTo;
   }
}

window.onload = init;
Back to top Go down
Sponsored content




Confirm fallow link Empty
PostSubject: Re: Confirm fallow link   Confirm fallow link Empty

Back to top Go down
 

Confirm fallow link

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» Link to left of image
» Need link to open in same window...
» Giving link to html file through xml
» How to call a static RDP link from web - works in development server, not IIS
» Changing link colours by adding CSS< not changing it? (Tumblr)

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: Confirm fallow link Edit-trash Useless :: Trash-