Log in

View Full Version : Javascript Help please :)



Lilian
04-07-2007, 04:25 PM
Hi,

I took the advice of other people in another thread and use javascript for a navigation. I have a small problem. I need to know how to set a default page like you do with iframes.

The javascript code is:

function showhide(id){
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
} else {
obj.style.display = "none";
}
}
}
var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n')
document.write('.submenu{display: none;}\n')
document.write('</style>\n')
}

function SwitchMenu(obj){
if(document.getElementById){
var el = document.getElementById(obj);
var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
if(el.style.display != "block"){ //DynamicDrive.com change
for (var i=0; i<ar.length; i++){
if (ar[i].className=="submenu") //DynamicDrive.com change
ar[i].style.display = "none";
}
el.style.display = "block";
}else{
el.style.display = "none";
}
}
}


function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(off set, end))
}
}
return returnvalue;
}

function onloadfunction(){
if (persistmenu=="yes"){
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=get_cookie(cookiename)
if (cookievalue!="")
document.getElementById(cookievalue).style.display ="block"
}
}

function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}

if (window.addEventListener)
window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
window.onload=onloadfunction

if (persistmenu=="yes" && document.getElementById)
window.onunload=savemenustate

function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_jumpMenuGo(selName,targ,restore){ //v3.0
var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}

var bustcachevar=1
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function dopage(url, containerid){
document.getElementById("content_main").innerHTML = '<div align="center"><img src="images/habboimages/CNY_KungFu_dude.gif" border="0" /><br><font size="1" color="white" face="verdana"><b>Finding</b> and <b>loading</b> page...</font></div>';
var page_request = false
if (window.XMLHttpRequest)
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar)
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
document.getElementById("content_main").innerHTML = '<div align="center"><img src="images/habboimages/CNY_KungFu_dude.gif" border="0" /><br><font size="1" color="white" face="verdana"><b>Finding</b> and <b>loading</b> page...</font></div>';
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=pag e_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){
if (file.indexOf(".js")!=-1){
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" "
}
}
}

function doupdate(update)
{
var title = document.getElementById('title');
title.innerHTML = update;
}

If I cannot do it that way how else would I do it or is there away of setting the <div> to do it.

Thanks for anyone who can help.

Colin-Roberts
04-07-2007, 04:36 PM
change center to the div name and change /home.php to your homepage

<body onLoad="LoadPage('/home.php','center');">

Lilian
04-07-2007, 04:42 PM
That does not work :(

Colin-Roberts
04-07-2007, 04:45 PM
link to script on dymanic drive?

Lilian
04-07-2007, 04:49 PM
Im not sure of it I was sent this by a friend ill try looking around Dynamic Drive now.

Dentafrice,
05-07-2007, 02:38 PM
http://www.habboxforum.com/showthread.php?t=342560&highlight=script+ajax

try that ;)

Lilian
05-07-2007, 04:09 PM
Nope =[

The page is loading when I click a link but isn't automatically loading when I load the page.

Ill tell you what I done:


<script>
//Set up variables
var xmlhttp ;
var thediv;
function LoadPage(page,usediv) {
// Set up request varible
try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");} catch (e) { alert("Error: Could not load page.");}
//Show page is loading
document.getElementById(usediv).innerHTML = 'Loading Page...';
//scroll to top
scroll(0,0);
//Store div in use
thediv = usediv;
//send data
xmlhttp.onreadystatechange = ShowPage;
xmlhttp.open("GET", page);
xmlhttp.send(null);
//Stop any link loading normaly
return false;
}
function ShowPage(){
//Check page is completed and there were no problems.
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
//Write data returned to page
document.getElementById(thediv).innerHTML = xmlhttp.responseText;
}
}
</script>

Ive put that in the head section of the code.


<a href="home.php" onclick="return LoadPage('home.php','content_main');">

I have placed that where my content would be.

I think I am doing it correctly anyway.

Dentafrice,
05-07-2007, 04:29 PM
did you make a div?

<div id="content_main"></div>

Lilian
05-07-2007, 04:45 PM
Yes, It still shows the template and the content bit is blank.

Dentafrice,
05-07-2007, 04:47 PM
Link me to the page and i will let firebug check it out

Lilian
05-07-2007, 04:50 PM
Ill PM you the link.

Colin-Roberts
05-07-2007, 05:31 PM
using that code you showed
this code should actually work?
<body onLoad="return LoadPage('home.php','content_main');">

Lilian
05-07-2007, 08:02 PM
Nope still does not work.

Btw Dentafrice, the link I sent you should know work.

Dentafrice,
05-07-2007, 08:04 PM
<body onLoad="return LoadPage('home.php','content_main');">

it needs to be at the top

Lilian
05-07-2007, 08:06 PM
BINGO!

Yes. Thank you very very much! Rep is on your way :)

Thread Closed. Problem Solved.

Want to hide these adverts? Register an account for free!