Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java Script is preventing page from loading, how to fix?

loading waiting timeout problem document.write error slow chrome

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 elliottveares

elliottveares

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 01 December 2015 - 08:30 AM

I have a free-counters.co.uk based JavaScript counter on my site, but I have a rather big problem of that if the counter can't load for what ever reason, it in turn prevents my page from loading properly in chrome in a timely fashion.

 

In essence if the counter can't load, the swirly circle on the chrome tab goes round and round endlessly until the script times out after about a minute. My script is below:

var count = "MYCOUNTERNAME";          // Change Your Account?
var type = "ornamentblue";       // Change Your Counter Image?
var digits = "6";          // Change The Amount of Digits on Your Counter?
var prog = "unique";          // Change to Either hit/unique?
var statslink = "no";    // provide statistical link in counter yes/no?
var sitelink = "no";     // provide link back to our site;~) yes/no?
var cntvisible = "yes"; // do you want counter visible yes/no?

var mode = "java";
var baseURL = "http://006.free-counters.co.uk/count-035.pl";
var GlobStr = '?count='+count;
GlobStr = GlobStr+'&type='+type;
GlobStr = GlobStr+'&digits='+digits;
GlobStr = GlobStr+'&prog='+prog;
GlobStr = GlobStr+'&statslink='+statslink;
GlobStr = GlobStr+'&sitelink='+sitelink;
GlobStr = GlobStr+'&cntvisible='+cntvisible;
GlobStr = GlobStr+'&mode='+mode;


document.write('<script language=JavaScript src='+baseURL+GlobStr+'></script>');

Now there must be a better way of doing this without using document.write, but I don't know how. Also I need to ensure that should the counter not be able to be loaded for what ever reason, that the main web page that it is implemented on continues loading as normal and does not wait endlessly until the script times out.

 

Regards: Elliott



#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 05 December 2015 - 07:58 PM

1) You're forgetting to quote the attributes.

 

Wrong:

<script language=JavaScript src=http://www.example.com/script.js>

 

Right:

<script language="javascript" src="http://www.example.com/script.js">

 

 

2) Don't use document.write() like that, it's outdated and prone to errors if your HTML has bugs in it. Try this:

var script = document.createElement('script');

script.setAttribute('src', baseURL + GlobStr);
script.setAttribute('type', 'text/javascript');

document.body.appendChild(script);

Also, the language attribute isn't really used anymore. Use the type attribute.


Edited by dargueta, 05 December 2015 - 07:59 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 elliottveares

elliottveares

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 05 December 2015 - 10:33 PM

It's ok, I've found my own in house solution using PHP. The code I am now using is below.

<?php
error_reporting(E_ALL ^ E_NOTICE);
header("Content-Type: text/html");


$con=mysqli_connect("127.0.0.1","grottotree","DATABASE-PASSWORD","geo_location");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL,: " . mysqli_connect_error() . "<br> <br> Also check login details in php file against database.";
}


$query = mysqli_query($con,"SELECT * FROM 2015_cords");

 

while($row = mysqli_fetch_array($query)) 
{
  $raw_results[] = "{la:".$row['latitude'] . ",lo:" . $row['longitude']."},";
  
}


natsort($raw_results);
$filterd_data = array_unique($raw_results);


$unique_number = count($filterd_data);

$exploded_count = str_split($unique_number);  //replace with $unique_number



$num_1 = print_r($exploded_count[0], true);
$num_2 = print_r($exploded_count[1], true);
$num_3 = print_r($exploded_count[2], true);
$num_4 = print_r($exploded_count[3], true);
$num_5 = print_r($exploded_count[4], true);
$num_6 = print_r($exploded_count[5], true);
$num_7 = print_r($exploded_count[6], true);
$num_8 = print_r($exploded_count[7], true);


if (strlen($unique_number)==1)

{

echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/'.$num_1.'.gif" alt="1st-Digit">';

}

else if (strlen($unique_number)==2)


{
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/'.$num_1.'.gif" alt="1st-Digit">';
echo '<img src="../gifs/'.$num_2.'.gif" alt="2nd-Digit">';

}

else if (strlen($unique_number)==3)

{

echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/'.$num_1.'.gif" alt="1st-Digit">';
echo '<img src="../gifs/'.$num_2.'.gif" alt="2nd-Digit">';
echo '<img src="../gifs/'.$num_3.'.gif" alt="3rd-Digit">';

}else if (strlen($unique_number)==4)


{

echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/'.$num_1.'.gif" alt="1st-Digit">';
echo '<img src="../gifs/'.$num_2.'.gif" alt="2nd-Digit">';
echo '<img src="../gifs/'.$num_3.'.gif" alt="3rd-Digit">';
echo '<img src="../gifs/'.$num_4.'.gif" alt="4th-Digit">';

}
else if (strlen($unique_number)==5)


{
echo '<img src="../gifs/0.gif" alt="Filler Didget">';
echo '<img src="../gifs/'.$num_1.'.gif" alt="1st-Digit">';
echo '<img src="../gifs/'.$num_2.'.gif" alt="2nd-Digit">';
echo '<img src="../gifs/'.$num_3.'.gif" alt="3rd-Digit">';
echo '<img src="../gifs/'.$num_4.'.gif" alt="4th-Digit">';
echo '<img src="../gifs/'.$num_5.'.gif" alt="5th-Digit">';

}
else if (strlen($unique_number)==6)


{
echo '<img src="../gifs/'.$num_1.'.gif" alt="1st-Digit">';
echo '<img src="../gifs/'.$num_2.'.gif" alt="2nd-Digit">';
echo '<img src="../gifs/'.$num_3.'.gif" alt="3rd-Digit">';
echo '<img src="../gifs/'.$num_4.'.gif" alt="4th-Digit">';
echo '<img src="../gifs/'.$num_5.'.gif" alt="5th-Digit">';
echo '<img src="../gifs/'.$num_6.'.gif" alt="6th-Digit">';

}


mysqli_close($con);
?>