Jump to content

Javascript Jah.js Ajax? I need a hand on this one


binxalot

Recommended Posts

I'm having a mental block, I'm ok with my php coding but when I run into javascript my mind can't handle it, I don't have enough experience with it to use it effectively, but it allows for loading content without a refresh.  So I ran into the jah.js file and how to use it to load pages into a site:

 

Code of the jah.js is this:

 

function jah(url,target) {

    // native XMLHttpRequest object

    document.getElementById(target).innerHTML = '';

    if (window.XMLHttpRequest) {

        req = new XMLHttpRequest();

        req.onreadystatechange = function() {jahDone(target);};

        req.open("GET", url, true);

        req.send(null);

    // IE/Windows ActiveX version

    } else if (window.ActiveXObject) {

        req = new ActiveXObject("Microsoft.XMLHTTP");

        if (req) {

            req.onreadystatechange = function() {jahDone(target);};

            req.open("GET", url, true);

            req.send();

        }

    }

}   

 

function jahDone(target) {

    // only if req is "loaded"

    if (req.readyState == 4) {

        // only if "OK"

        if (req.status == 200) {

            results = req.responseText;

            document.getElementById(target).innerHTML = results;

        } else {

            document.getElementById(target).innerHTML="jah error:\n" +

                req.statusText;

        }

    }

}

 

You need to add this to before the head tag

<script language="javascript1.3" src="jah.js" ></script>

 

Then this to dictate where content gets loaded

<div id="content"></div>

 

Then you use this as the link code for text or image links

javascript:jah('contact.php','content');

 

This all works fine and good, I use it because an mp3 player wont skip if you change content in the page since the page wont refresh itself.  The problem I have is this.  I can't get static links for the pages anymore.  Since the content for each page loads into the <div> tag I end up with only an index.php page and no subpages like you would find on a normal site.  What I'm looking to do is add in some javascript or php code that uses a $_GET['page']; for hard links to pages on the site.

 

So if I want to load the contact page I would do  index.php?page=contact

 

Then I my code I would do:

<?

 

if(isset($_GET['page'])){

 

$page = $_GET['page'];

 

if($page == "contact"){

print "<script language=\"JavaScript\">";

print "javascript:jah('contact.php','content');";

print "</script>";

}

 

 

}

?>

This doesnt work, I want the  javascript:jah('contact.php','content' to run the jah.js file when a person uses this link in their address bar: index.php?page=contact .

 

Can someone tell me what I need do to get this to work?

 

Thanks,

-Binx

Link to comment
Share on other sites

 

okay, I looked through the board and found a similar post, it fixed my problem.

 

If anyone else is running into the same problem, I used this as a fix:

 

<?

 

if(isset($_GET['page'])){

 

$page = $_GET['page'];

 

if($page == "contact"){

print "<script language=\"JavaScript1.3\">";

print "setTimeout(\"jah('contact.php','content');\",10);";

print "</script>";

}

}

?>

 

the setTimeout will run the jah command after 10 miliseconds and will load the contact page into the <div> tag.

 

Good stuff.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.