Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2012, 04:05 PM   PM User | #1
erebus
New Coder

 
Join Date: Sep 2012
Location: Houston, TX
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
erebus is an unknown quantity at this point
JavaScript include difficulties

I have small peice of code that runs at the end of my code after the page is created and it manipulates the sidebar which is apart of the overall template. Since its accessing an API i could foresee future changes that would need to be made to the code and ofcourse that needs to be reflected to all pages. So i took my peice of code

Code:
<!-- Top 3 Streams -->
<script type="text/javascript">

var myurl = encodeURIComponent("https://api.twitch.tv/kraken/streams?game=DayZ&limit=3")

scr = document.createElement("script");
scr.type = "text/javascript"
scr.src = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22' + myurl + '%2F%22&format=json&callback=showGame';
document.body.appendChild(scr);
</script>
<!-- / Top 3 Streams -->
and replaced it with an include

Code:
<!-- Top 3 Streams -->

    <?php include('includes/top3streams.php'); ?>

<!-- / Top 3 Streams -->
and now its not working properly... did i not implement this include properly?

The contents of the include file read

Code:
<script type="text/javascript">

var myurl = encodeURIComponent("https://api.twitch.tv/kraken/streams?game=DayZ&limit=3")

scr = document.createElement("script");
scr.type = "text/javascript"
scr.src = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22' + myurl + '%2F%22&format=json&callback=showGame';
document.body.appendChild(scr);
</script>
perhaps the <script type="text/javascript"> and </script> needs to remain in the HTML? perhaps its not loading in fast enough? any thoughts?

perhaps i should be using a External JS file instead? or heck, maybe even a more basic HTML include?
erebus is offline   Reply With Quote
Old 10-08-2012, 04:08 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
No, the script tags should remain in the included file.

What error messages is the browser producing?
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 10-08-2012, 04:09 PM   PM User | #3
erebus
New Coder

 
Join Date: Sep 2012
Location: Houston, TX
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
erebus is an unknown quantity at this point
Note: It works perfectly in Chrome, but not in IE8
erebus is offline   Reply With Quote
Old 10-08-2012, 04:10 PM   PM User | #4
erebus
New Coder

 
Join Date: Sep 2012
Location: Houston, TX
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
erebus is an unknown quantity at this point
Quote:
Originally Posted by WolfShade View Post
No, the script tags should remain in the included file.

What error messages is the browser producing?
Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Timestamp: Mon, 8 Oct 2012 15:09:40 UTC


Message: Unknown runtime error
Line: 39
Char: 7
Code: 0
URI: http://dayzlive.com/beta/
erebus is offline   Reply With Quote
Old 10-08-2012, 04:19 PM   PM User | #5
erebus
New Coder

 
Join Date: Sep 2012
Location: Houston, TX
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
erebus is an unknown quantity at this point
It works perfectly in Chrome

Not at all in IE8

and intermittently in Firefox (when refreshing it sometimes dident work)

*edit: i refreshed over a dozen times and it suddenly seems stable in firefox as well now


Note the function it references is not being included.. its written directly in head

Code:
function showGame(o)
{
   var thediv = document.getElementById("results")
   var base = o.query.results.json.streams;
   for (var i = 0; i < base.length; i ++ )
   {
      document.getElementById("streamlogo" + (i + 1)).src = base[i].channel.logo;
      document.getElementById("streamname" + (i + 1)).innerHTML = base[i].channel.name;
      var status = base[i].channel.status
      if (status.length > 76)
      {
      var statusubstr = status.substr(0,76)
      document.getElementById("streaminfo" + (i + 1)).innerHTML = "<b>Viewers: " + base[i].viewers + "</b><br><p>" + statusubstr + "... </p>";
      }
      else
      {
      document.getElementById("streaminfo" + (i + 1)).innerHTML = "<b>Viewers: " + base[i].viewers + "</b><br><p>" + status + " </p>";
      }
   }
}

Last edited by erebus; 10-08-2012 at 04:23 PM..
erebus is offline   Reply With Quote
Old 10-08-2012, 04:32 PM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
presumably you are using the include in the head.

but you are attaching the script to the body.

when the browser is reading the head section, the body does not exist yet. In the absence of a body, most browsers will create one, because they know that there has to be one, eventually.

But IE is fun and quirky and likes to do its own thing.

The easiest solution (I think) is to attach the script to the head, using this line instead of the "body" one:
Code:
document.getElementsByTagName("head")[0].appendChild(scr);
xelawho is offline   Reply With Quote
Old 10-08-2012, 05:13 PM   PM User | #7
erebus
New Coder

 
Join Date: Sep 2012
Location: Houston, TX
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
erebus is an unknown quantity at this point
Quote:
Originally Posted by xelawho View Post
presumably you are using the include in the head.

but you are attaching the script to the body.

when the browser is reading the head section, the body does not exist yet. In the absence of a body, most browsers will create one, because they know that there has to be one, eventually.

But IE is fun and quirky and likes to do its own thing.

The easiest solution (I think) is to attach the script to the head, using this line instead of the "body" one:
Code:
document.getElementsByTagName("head")[0].appendChild(scr);
I think i am using the include in the body, actually its the last thing in the body.. either that or i do not quiet understand your recomendation

Code:
<!-- Top 3 Streams -->

    <?php include('includes/top3streams.php'); ?>

<!-- / Top 3 Streams -->

</body>
</html>

Last edited by erebus; 10-08-2012 at 08:29 PM..
erebus is offline   Reply With Quote
Old 10-08-2012, 08:38 PM   PM User | #8
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Why not just use an external JavaScript - there is no point in using a server side include call for a script when using an external script allows the code to be shared between pages in the browser with only doewnloading it once rather than once per page.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 10-08-2012, 11:39 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
What I *REALLY* don't understand is the need to use appendChild, at all.

Why not just do this directly????
Code:
<script type="text/javascript"
 src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22https%3A%2F%2Fapi.twitch.tv%2Fkraken%2Fstreams%3Fgame%3DDayZ%26limit%3D3%2F%22&format=json&callback=showGame">
</script>
That is, figure out what the URIEncoded version of myurl is ONE TIME and plunk it in place in the yahoo query url. Then you don't have to use an extra layer of indirection and appendChild and all the rest of it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:02 PM.


Advertisement
Log in to turn off these ads.