Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9288979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:02:50+00:00 2026-06-18T20:02:50+00:00

This thing is driving me mad… I have a simple js script wich checks

  • 0

This thing is driving me mad… I have a simple js script wich checks for all the links tag () in a page and check if two of those links at least correspond to a given href attribute.
If I put the script in the main page as an inline script it works great. If I put it inside an external .js script on the same page, it doesn’t find any element. Just like if it’s only scanning its internal ones but not the parent document ones.
the script is this

            var as,i,islink,l1,l2;
        l1 = false
        l2 = false
        as=document.getElementsByTagName('a');
        for(i=0;i<as.length;i++)
        {
            islink=as[i].href;
alert(islink);

            if(islink == 'http://www.linktocheck.com/')
            {
                if(!as[i].getAttribute('rel')) {l1 = true;}
            }
            if(islink == 'http://www.linktocheck.com/somedir/somepage.asp')
            {
                if(!as[i].getAttribute('rel')) {l2 = true;}
            }
        }
        if(!l1 || !l2) {alert('links not found in the page');}

I put alert(islink); for debug purposes to see if some link element has been found in the page or not.
The HTML of the calling page is this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"><title>page a</title>
<meta name="robots" content="noarchive" />
<meta content="text/html; charset=iso-8859-1" http-equiv="content-type" /></head>
<body>
<script src="externaljstochecklinks.js?PARAMETER=somevalue" id="uniqueid" type="text/javascript"></script><div style="margin-top: 6px;margin-bottom: 4px;text-align: center;"><a href="http://www.linktocheck.com/somedir/somepage.asp" title="" target="_blank">[...]</a>&nbsp;&nbsp;&nbsp;<a href="http://www.linktocheck.com/" title="" target="_blank"><img src="http://www.linktocheck.com/skins/some/images/some.gif" alt="" style="border: none;vertical-align: middle;" /></a></div>

Why if I put the link check script inline it finds the elements if I put it as external it doesn’t?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:02:51+00:00Added an answer on June 18, 2026 at 8:02 pm

    You have to wait for the DOM to finish loading before you can query it to find DOM elements.

    Your script appears to be at the beginning of the <body> section which means it will execute BEFORE the DOM has been parsed and loaded. Thus, there is nothing yet loaded to find.

    The simplest fix would be to move your <script> tag to the end of the body right before the </body> tag. You could also use an event listener and execute your code only when the event fires indicating that the DOM is ready.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe it is just a simple mistake but this thing is driving me crazy.
This whole Facebook access_token thing is driving me nuts. All I want to do
This is driving me absolutely bonkers, and it seems like such a simple thing
This thing is driving me nuts. I have a UserControl that is called WebUserControl.
I've been beating around on this thing all day and it's driving me nuts!
This has been driving me mad for months: I have a little app to
I have a simple JavaScript thing that's driving me bonkers. Instead of the output
Calling all cross-compatibility experts! Please check out this page on my site, as it
This is driving me a little bit mad! I have taken an image and
This is driving me mad, I have a facebook like button on my site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.