View Full Version : javascript help
Alan92RTTT
04-17-2012, 02:57 PM
I'm having an issue with this code
<head>
<style>
div.div_3sg
{
width: 100%;
}
div.tab_3sg
{
border: 1px solid;
margin: auto;
clear: both;
height: 120px;
width: 98%;
padding-left: 4px;
}
#tabs li {
list-style: none outside none;
}
#tabs ul {
padding: 0;
border-bottom: 0 none !important;
}
#tabs li, #tabs li a {
float: left;
}
#tabs ul li{
font-weight: bold;
padding: 8px;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<script type="text/javascript">
document.write("\<script src='//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'>\<\/script>");
</script>
<body>
<div id="tabs" class="div_3sg">
<div style="display: block; float: left; ">
<ul>
<script type="text/javascript">
jQuery.getJSON( "include_js.php",function(data){
jQuery.each(data.groups, function(id, group){
var a=1;
document.writeln('<li id="#tab-'+(id+1)+'">A'+group.name+'</li>');
});
});
</script>
</ul>
</div>
</div>
</body>
That code produces the following result.
<li id="#tab-1">AEvents</li>
<li id="#tab-2">AGroups</li>
<li id="#tab-3">AInformation</li>
Can anyone see why its freaking out and clearing everything off the page.
VR4Rob
04-17-2012, 04:58 PM
Looks like you missed a </div> tag on the tag before the </body> tag.
Alan92RTTT
04-17-2012, 05:37 PM
Fixed the div tag, no change.
If it helps in FF the spinner on the tab keeps spinning.
I've got to get some nocache headers in it. It runs once, fails and I can't open fire bug on it and get it to rereun without closing the tab and entering it from the address bar(no shift-refresh does not work)
Alan92RTTT
04-17-2012, 05:40 PM
uploaded here if anyone wants to look
http://3sg.org/beta/3sg_footer.htm
stealthify
04-18-2012, 06:17 AM
Give this a try:
<head>
<style>
div.div_3sg
{
width: 100%;
}
div.tab_3sg
{
border: 1px solid;
margin: auto;
clear: both;
height: 120px;
width: 98%;
padding-left: 4px;
}
#tabs li {
list-style: none outside none;
}
#tabs ul {
padding: 0;
border-bottom: 0 none !important;
}
#tabs li, #tabs li a {
float: left;
}
#tabs ul li{
font-weight: bold;
padding: 8px;
text-decoration: none;
cursor: pointer;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<div id="tabs" class="div_3sg">
<div id="tabHeader" style="display:block; float:left;">
<!-- Tabs Generated Here -->
</div>
</div>
<script>
$.getJSON('3sgTabs.json', function(data) {
var items = [];
$.each(data.groups, function(id, group) {
items.push('<li id="tab-' + (id+1) + '">' + group.name + '</li>');
});
$('<ul/>', {
'class': 'tabCategories',
html: items.join('')
}).appendTo('#tabHeader');
});
</script>
</body>
Output generated is:
<div id="tabs" class="div_3sg">
<div id="tabHeader" style="display:block; float:left;">
<ul class="tabCategories">
<li id="tab-1">Events</li>
<li id="tab-2">Groups</li>
<li id="tab-3">Information</li>
</ul>
</div>
</div>
Working demo:
http://www.stealthify.com/3sgto/projects/3sgTabs.php
Alan92RTTT
04-18-2012, 08:18 AM
Can you explain why what I did , did what it did?
stealthify
04-18-2012, 09:05 AM
I didn't look too far into it. I just recreated it from scratch with how I would do it, and then adapted it to what you were trying to accomplish.
Looking at it a little closer now, your problem line is using document.writeln to begin writing to the document and then not using the document.close() method to close the output stream.
Alan92RTTT
04-18-2012, 09:16 AM
I originally used document.write() would that still need the close?
stealthify
04-18-2012, 09:30 AM
Yep, that's pretty much identical to .writeln, except .writeln will put a new line after it. Both open and write to the output stream.
Once all the writes are performed, the document.close() method causes any output written to the output stream to be displayed.
Alan92RTTT
04-18-2012, 09:33 AM
wonder if thats what broke it.
I'll have to try it at lunch
I'll probably use yours tho I like it better.
stealthify
04-18-2012, 09:36 AM
Should do the trick, and hope that helps. Let me know how it goes.
Alan92RTTT
04-18-2012, 09:39 AM
thanks. I will
Alan92RTTT
04-18-2012, 02:39 PM
(*%$)*$)(*#(*
I got it working, I changed it a bit and it still worked.
I added the code to add the links and %_*)*)*(%^#
<head>
<style>
div.div_3sg
{
width: 100%;
}
div.tab_3sg
{
border: 1px solid;
margin: auto;
clear: both;
height: 120px;
width: 98%;
padding-left: 4px;
}
#tabs li {
list-style: none outside none;
}
#tabs ul {
padding: 0;
border-bottom: 0 none !important;
}
#tabs li, #tabs li a {
float: left;
}
#tabs ul li{
font-weight: bold;
padding: 8px;
text-decoration: none;
cursor: pointer;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<div id="tabs" class="div_3sg">
<div id="tabHeader" style="display: block; float: left; ">
<ul id="tabUl" >
<!-- Tabs Generated Here -->
</ul>
</div>
<!-- Tabs Generated Here -->
</div>
<div id="hidden" style="display:none" class="user_css"><label id="text_color">text</label><a id="link_color" href="#">text</a></div>
</body>
<script>
$(document).ready(function(){
$.getJSON( "include_js.php",function(data){
var items = [];
$.each(data.groups, function(id, group) {
$('<li id="#tab-'+(id+1)+'">'+group.name+'</li>').appendTo('#tabUl');
var div = '<div id="tab-'+(id+1)+'" class="tab_3sg">';
div = div +'<b>'+group.title+'</b><br>';
$.each(group.links, function(id, link) {
div = div + '<a href="'+link.url+'">'+link.name+'</a><br>';
});
div = div + '</div>';
$(div).appendTo('#tabs');
});
});
$('#tabs .tab_3sg').hide();
$('#tabs .tab_3sg:first').show();
$('#tabs ul li:first').addClass('active');
var text_color = $('#text_color').css("color");
var link_color = $('#link_color').css("color");
$('#tabs ul li').css('color',link_color);
$('#tabs ul li:first').css('color', text_color);
$('#tabs ul li').click(function(){
$('#tabs ul li').removeClass('active');
$(this).addClass('active');
var currentTab = $(this).attr('id');
$('#tabs .tab_3sg').hide();
$(currentTab).show();
$('#tabs ul li').css('color', $('#link_color').css("color"));
$(this).css('color', $('#text_color').css("color"));
return false;
});
});
</script>
You can see what its doing on http://3sg.org/beta/3sg_footer.htm
HilbillyHomeboy
04-18-2012, 03:23 PM
You can see what its doing on http://3sg.org/beta/3sg_footer.htm
The bottom cell, the 1st clickable link is underneath of the header text for that cell. Can not click on it. I'm viewing with Chrome.
Jeremy
Alan92RTTT
04-18-2012, 03:39 PM
if the javascript was working that would not be an issue :p
That page should look/work like the block at the bottom of the page here when its done.
stealthify
04-18-2012, 06:22 PM
(*%$)*$)(*#(*
I got it working, I changed it a bit and it still worked.
I added the code to add the links and %_*)*)*(%^#
So close. Now the problem is the div.tab_3sg CSS parameter. You have it set to a static height of 120px, when perhaps it might be better suited to use a min-height:120px instead.
Alan92RTTT
04-18-2012, 08:58 PM
Height is not the issue unless the JS is working in your browser.
this part of the JS is not running in FF
$('#tabs .tab_3sg').hide();
$('#tabs .tab_3sg:first').show();
$('#tabs ul li:first').addClass('active');
var text_color = $('#text_color').css("color");
var link_color = $('#link_color').css("color");
$('#tabs ul li').css('color',link_color);
$('#tabs ul li:first').css('color', text_color);
$('#tabs ul li').click(function(){
$('#tabs ul li').removeClass('active');
$(this).addClass('active');
var currentTab = $(this).attr('id');
$('#tabs .tab_3sg').hide();
$(currentTab).show();
$('#tabs ul li').css('color', $('#link_color').css("color"));
$(this).css('color', $('#text_color').css("color"));
return false;
});
stealthify
04-18-2012, 09:08 PM
Height is an issue when you are programmatically trying to cram more vertical content into a div than you have made room for via CSS.
I know this because the text overlap under your third header is from the runoff of the div above it.
I have updated my version to demonstrate/help:
http://stealthify.com/3sgto/projects/3sgTabs.php
stealthify
04-18-2012, 09:16 PM
But I see what you're saying. Your JavaScript wasn't working in my browser, but even so you would still have a runoff outside of the container div.
The version I just posted should solve that. Hope that helps.
Alan92RTTT
04-18-2012, 09:53 PM
Runoff issue fixed in my version.
Alan92RTTT
04-18-2012, 10:05 PM
you still have AIM?
FYI, you pulled some of the stuff I did with color. I did that so it would pick up the color for what ever page it on. I did not want the tab/links hard coded for color.
Alan92RTTT
04-18-2012, 10:21 PM
I think I see the problem. The appendTo's are not done rendering when the last block of JS is run and its errors. I need a way to make it wait for them.
Alan92RTTT
04-18-2012, 11:41 PM
I think I have it.
Now the next step. I need to be able to include it in an html page with a script tag. fun...
stealthify
04-19-2012, 01:49 AM
I do still have AIM, Shaun I Am.
I'll keep troubleshooting with you. I think you're in the home stretch now though.
Alan92RTTT
04-20-2012, 10:16 AM
I'm having an issue with the json call.
I've moved the html to http://mi3si.org/beta/3sg_footer-2.htm because I am trying to test loading the json from a separate server using jsonp.
You can see the json here http://3sg.org/beta/include_js.php
The JS is coming from 3sg.org and running with no issue until it throws a "missing variable name" error when processing the json results.
json call code.
var data = $.ajax({
url : 'http://www.3sg.org/beta/include_js.php',
dataType : "jsonp",
jsonp: 'callback',
jsonpCallback: 'getData',
timeout : 10000,
success: function(){
alert("success");
}
});
function getData(data){
$('#tabHeader').text(data.message);
}
stealthify
04-20-2012, 08:58 PM
Hmm, if you want to use jsonp, here's how I did it:
3SG.org Tabs Demo (Remote Server) (http://www.portland3s.com/personal/projects/3sgTabsXserver.php)
My copy of the json file that the Portland3S domain is accessing can be found at:
http://www.stealthify.com/3sgto/projects/3sgTabsXserver.php
Does it work for you?
Alan92RTTT
04-21-2012, 12:41 AM
oh this is frustrating. My code with your include works. With mine(which seems to return the same thing) does not work.
ARG.
<?php
$data = array(
"groups"=> array(
array(
"name" => "Events",
"title" => "3000GT/Stealth/GTO Event Pages",
"links" => array(
array("name"=>"3S National Gathering","url"=>"http://www.3sng.org"),
array("name"=>"East Coast Gathering","url"=>"http://www.3secg.com"),
array("name"=>"Upper Mid-West Gathering","url"=>"http://umg.mn3s.org"),
array("name"=>"Blue Ridge Gathering","url"=>"www.blueridgegathering.com"),
),
),
array(
"name" => "Groups",
"title" => "3000GT/Stealth/GTO Clubs and Groups",
"links" => array(
array("name"=>"Michigan 3S","url"=>"http://www.mi3si.org"),
array("name"=>"MInnesota 3S","url"=>"http://www.mn3s.org"),
array("name"=>"Wisconsin 3S","url"=>"http://www.w3si.org"),
array("name"=>"3000GT/Stealth/GTO Forums","url"=>"www.3sgto.org"),
array("name"=>"Texas 3S","url"=>"www.texas3s.com"),
array("name"=>"3000GT/Stealth International","url"=>"www.3sl.org"),
),
),
array(
"name" => "Information",
"title" => "3000GT/Stealth/GTO Information and Resources",
"links" => array(
array("name"=>"Stealth 316","url"=>"http://www.3sng.org"),
array("name"=>"3000GT / Stealth / GTO Information","url"=>"www.3sg.org"),
),
),
),
);
$json_data = json_encode($data);
echo '<pre>('. $json_data.');</pre>';
stealthify
04-21-2012, 01:07 AM
Remove the pre tags, and designate a callback function (or use the automatically generated one):
<?php
// Build Links Array
$data = array(
"groups"=> array(
array(
"name" => "Events",
"title" => "3000GT/Stealth/GTO Event Pages",
"links" => array(
array("name"=>"3S National Gathering","url"=>"http://www.3sng.org"),
array("name"=>"East Coast Gathering","url"=>"http://www.3secg.com"),
array("name"=>"Upper Mid-West Gathering","url"=>"http://umg.mn3s.org"),
array("name"=>"Blue Ridge Gathering","url"=>"http://www.blueridgegathering.com"),
),
),
array(
"name" => "Groups",
"title" => "3000GT/Stealth/GTO Clubs and Groups",
"links" => array(
array("name"=>"Michigan 3S","url"=>"http://www.mi3si.org"),
array("name"=>"MInnesota 3S","url"=>"http://www.mn3s.org"),
array("name"=>"Wisconsin 3S","url"=>"http://www.w3si.org"),
array("name"=>"3000GT/Stealth/GTO Forums","url"=>"http://www.3sgto.org"),
array("name"=>"Texas 3S","url"=>"http://www.texas3s.com"),
array("name"=>"3000GT/Stealth International","url"=>"http://www.3si.org"),
),
),
array(
"name" => "Information",
"title" => "3000GT/Stealth/GTO Information and Resources",
"links" => array(
array("name"=>"Stealth 316","url"=>"http://www.3sng.org"),
array("name"=>"3000GT / Stealth / GTO Information","url"=>"http://www.3sg.org"),
),
),
),
);
// Encode Links Array to JSON
$json_data = json_encode($data);
// Collect & Sanitize Random callback parameter
$callback = preg_replace("/[^a-zA-Z0-9\.\_\[\]]+/", "", $_GET['callback']);
// Output Results
echo $callback.'('.$json_data.');';
Alan92RTTT
04-21-2012, 01:16 AM
ah you have some extra stuff in your include ;)
I had a feeling that was the case. I added the pre's because they came out if I pulled your file up in a browser
AWESOME
http://mi3si.org/beta/3sg_footer-2.htm
stealthify
04-21-2012, 01:19 AM
Nice! Glad it worked.
Enjoy.
stealthify
04-21-2012, 01:27 AM
By the way, one thing I've been meaning to mention:
For some of your links, you don't list the fully qualified (absolute) URL. So, for example, 3sg.org links to "http://mi3si.org/beta/www.3sg.org" instead. It happens on the Texas 3S, 3SI, 3SGTO, 3SG and Blue Ridge Gathering links. Just thought I'd give you a heads up in case you hadn't noticed yet.
Alan92RTTT
04-21-2012, 02:11 AM
The array is temporary until I have time to make the database to store them. That code will ensure that all url's have http on them before they are put in the array.
Thanks a ton.
NOw I just need to get the code to load jquery in plain JS working so the HTML just needs one script tag
Alan92RTTT
04-21-2012, 04:18 PM
I've tried several variation on this to get jquery to load within an js file.
function getScript(url,success){
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= url;
script.async = true;
document.body.appendChild(script);
}
var jqueryUrl = "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
getScript(jqueryUrl);
The JS is loaded into the document but it does not initialize.
Alan92RTTT
04-21-2012, 04:25 PM
Before you ask, I am trying to get this all done in one call.
<script src="http://www.3sg.org/beta/3sg_footer.js"></script>
stealthify
04-21-2012, 08:54 PM
Before you ask, I am trying to get this all done in one call.
<script src="http://www.3sg.org/beta/3sg_footer.js"></script>
Done.
3SG.org Tabs Demo (Source) (http://www.stealthify.com/3sgto/projects/3sgTabs.php)
3SG.org Tabs Demo (Remote Server) (http://www.portland3s.com/personal/projects/3sgTabs.php)
Alan92RTTT
04-21-2012, 09:35 PM
Can you explain a few things?
Everything I have read says you should not use document.write yet you did and it works fine.
Whats with the "scr"+"pt" in the document writes?
stealthify
04-21-2012, 09:48 PM
Can you explain a few things?
Everything I have read says you should not use document.write yet you did and it works fine.
Whats with the "scr"+"pt" in the document writes?
Old habits die hard - the scr + pt is legacy code for older browsers. It was taught to me when I was first getting started, but it can be changed to your way and should work the same. Here's some more details:
document.write('<SCR' + 'IPT>'), why? JavaScript and AJAX forum at WebmasterWorld (http://www.webmasterworld.com/forum91/2913.htm)
I've updated my .js file to use the createElement + appendChild methods instead, if that's what you'd prefer to go with.
Alan92RTTT
04-21-2012, 10:06 PM
Well i have a working page. I'm not sure why some things are working as its written and not the way they were but it looks to be working.
NOt sure why the document.ready code had to be put in a function and call the way it is.
stealthify
04-21-2012, 10:13 PM
I'm not a fan of scripting languages :( But with the help of jQuery, etc, it has come a long way.
Glad to see it all working!
Alan92RTTT
04-21-2012, 10:14 PM
Workign from 3sg.org - 3000GT / Stealth / GTO information (http://www.3sg.org) and Michigan 3SI (http://www.mi3si.org) yay!
Thanks alot.
stealthify
04-21-2012, 10:15 PM
No prob!
Alan92RTTT
04-26-2012, 10:30 AM
CRAP CRAP CRAP.
After all that work google does not see the rendered results of all that JS. I'll need to go back to a server side version to get search engines to see it.
Guess I get to make my 1st VBB plugin ;)
DrGonzo
04-26-2012, 11:13 AM
Wonder why Google is not seeing it.....The bot should just be parsing the rendered source code on the page. Regardless of the contents being hidden by CSS the code is all still there.
The JS version is perfect as it can be added to any site and still function. VBB plugin will only work with VBB so all other sites will need to have a one off solution......
Alan92RTTT
04-26-2012, 11:23 AM
looking at the google webmaster tools google is not parsing the JS at all. It does not even load it. This is based on a review of 3sg.org so its not a crossdomain issue.
I went with JS because of the issues you point out. I can setup test SMF and phpBB boards so I can make a plugin for them too.
DrGonzo
04-26-2012, 12:33 PM
Yeah I just pulled the page into Lynx and it's not actually writing like I thought..... But Google does parse links embedded in Javascript and Ajax, so wonder if it's just a WM Tools issue than?!?!?
Alan92RTTT
04-26-2012, 12:58 PM
If it was just a WM thing I should be seeing inbound links to 3sg.org
stealthify
04-26-2012, 06:32 PM
Well that's a pain. :( We should write Google a strongly-worded letter.
Alan92RTTT
05-02-2012, 02:48 PM
I have a PHP version working http://www.3sg.org/footer/footer.php
I have a vbb 4 plugin for it. I just need someone brave enough to test it ;)
DrGonzo
05-02-2012, 05:09 PM
Installed and working correctly on VBB You can see it running on http://colorado3s.org
I checked the source code and all of the links are writing out correctly now. So GA should start picking them up. Took less than 5 min to install the plugin.
Good Job Alan!!!
Alan92RTTT
05-02-2012, 05:52 PM
Now to make one for vb 3, SMF and the phpBB variants :D
Powered by vBulletin® Version 4.2.3 Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.