PixelPaul's avatar

load in page content and new JS functions for each new page/link and remove old.

So i am trying to build an app that when the use clicks on a menu item istead of just loading the new page it will just get the inner contents of the new page and swap out the current contents with the new contents. The problem i have found is that the old javascript functions from the old content is still active in memory. So, if i had some hook functions or timer functions or some functions that don't get swapped with new functions with the same name, they will still be there.

Is there i was way i should go about loading in JS functions and code for each page content but then remove them when the page content is replaced.

And having said that, i would maybe like to keep some functions still running if possible if i swap out the page content. example: I user is uploading a big file but they want to browse to some other pages, so i want that ajax function to keep uploading the file.

0 likes
11 replies
jlrdw's avatar

Typically you replace the content in a division with the new content.

PixelPaul's avatar

erm, yes that is what i will do. but i want to know about the JS code and functions.

jlrdw's avatar

There are tons of JS tutorials here and on the web. I'd suggest starting out with jQuery it is probably easier catch on to at first.

PixelPaul's avatar

yes i am using jquery. i am not sure you understand my questions tho.

jlrdw's avatar

When you replace content as an example in a division there shouldn't be any content left in memory the content is replaced.

PixelPaul's avatar

javascript functions stay active and in memory even when the script code is removed from the html. do you understand this?

jlrdw's avatar

It's no different than say x-editable and in place editing. Yes the remnants of the "what happened" part of the function may or may not be stuck somewhere in memory, but the content is changed.

Div first time = "hello world"

Div second time = "hello there"

Otherwise in place edits would not work.

Sometimes it's good to "reset" a var to '' (blank) if needed.

PixelPaul's avatar

Let me give an example so you understand.

If I have this..

Some content here Function myfunction(){ Alert('hello'); }

I then replace the contents of that div with all new HTML and some scripts.. that function will no longer be there. But I can still call it and it still works.

PixelPaul's avatar

Sorry the code did not come in. Here it is...

<div>
Some content here
<script>
Function myfunction(){
 Alert('hello');
}
</script>
<div>
jlrdw's avatar

Not this

<div>
Some content here
<script>
Function myfunction(){
 Alert('hello');
}
</script>
<div>

This

<div>
Some content here
<div>

///Put the script at bottom, not in a div
<script>
Function myfunction(){
 Alert('hello');
}
</script>

See this

<h1>Pets</h1>
<form method="post" action="<?php echo DIR; ?>owner/indexsch">
    <label>sch</label><input type="text" name="osch" value=""> <input type="submit" name="submit" value="Search">       
</form>

<table id="myTable">    
    <tr>

        <th style="width:40px;">ownerid</th>
        <th style="width:100px;">oname</th>
        <th>ostreet</th>
        <th>odate</th>
        <th>ocheck</th>
    </tr>
    <?php
    if ($data['owners']) {
        foreach ($data['owners'] as $row) {
            echo "<tr>";
            //echo "<td>" . $row->petid . "</td>";
            echo "<td>" . $row->ownerid . "</td>";
            echo "<td>" . $row->oname . "</td>";
            echo "<td>" . $row->ostreet . "</td>";
            echo "<td>" . $row->odate . "</td>";
            echo "<td>" . $row->ocheck . "</td>";

            //echo "<td><a href='".DIR."pet/edit?petid=".$row->petid."'>Edit2</a>
            //<a href='".DIR."pet/delete/$row->petid'>Delete</a></td>";
            echo "</tr>";
        }
    }
    ?>
</table>
<?php
echo '<table style="width:70%;">';
echo '<tr>';
echo '<td>' . $data['menu'] . '</td>';

echo '</tr></table>';
?>
<script>

$(function () {
    function makeAJAXRequest (text) {
        $.ajax({
            url: '<?php echo DIR . "owner/getowner"; ?>',
            type: 'GET',
            dataType: 'json',
            data: { id: text },
            success: processSuccess
        });
    }

    function processSuccess (data) {
    //alert(data);
        $.each(data, function (key, value) {
            //alert(value);
            var id = '#' + key,
                elem = window.opener.$(id);
                    //alert(id);
            if (key === 'ocheck') {
                elem.attr('checked', value === '1');
            }
            else {
                elem.val(value);
            }
        });

        self.close();
    }

    $("#myTable td:nth-child(1)").click(function (event) {
        event.preventDefault();

        var $td = $(this).closest('tr').children('td');
        var currentCellText = $td.eq(0).text();
        var CellText = $td.eq(1).text();

        makeAJAXRequest(currentCellText);
    });
});



</script>

Notice my js stuff is at bottom.

If you study the script, it is used to auto fill several text fields on an edit form. If you need to see it in action, I will display a video, let me know.

But this is just test data I use to test out frameworks, etc.

PixelPaul's avatar

You don't understand, I want to remove the JavaScript code too and have new js code without the old one still being active in browser even tho the code was removed.

Please or to participate in this conversation.