Question

-edited for clarity-

I have an index-page with a DIV, into which other main content (html-files) gets loaded (client-side) when the visitor clicks on a link (via AJAX, jquery load(), see code below).

I want to add a random background-image, ONLY on the initially loading page (i.e. in the body-tag), but as soon as some (other) content gets loaded into the "ajax" DIV, this background-image should disappear from body of index.html.

I would like to write an if-else condition which makes sure the content of that "ajax" DIV is still unchanged. I found window.location.hrefdoes not work to check the state of the site, as the resulting URL is always the same, independent of the AJAX loaded content.

Now, would there be a way to write a condition like:

- IF original DIV-content gets exchanged, => background : ' '

or

- IF DIV-content has/hasn't ID..., => background : ' '

I hope my problem can be understood.


EDIT: I add something which I hope helps a bit for clarification: my site (index.html), simplyfied, looks like this:

<head>  
</head>
<body>
    <a class="ajaxLink" href="othercontent.html">next</a>

        <div id="content-main">
            <div id="othercontent"></div> //<== this is content from other html-file
        </div>
</body>

The User clicks on the link, and then, using jquery load(), some html-file content gets loaded into the div (#content-main), and in the end everything only happens in this single index.html. So if I assign a background-image to the body of the index.html (using jquery $('body').css...), I need to find a way to identify when non-initial content exists/gets loaded in the DIV #content-main..


EDIT2: I will now paste the mentioned jQuery code here, as it should help to understand why some suggestion from the answers do not work (don't get confused, the code contains both the ajax load() for new content, AND many lines for HTML5 history functionality):

$(function () {
//AJAX: load html-pages dynamically into this site's div (#content-main), and add browser history and URL support

    /*unnecessary? I don't use the next 3 lines of code
    //To override the default action for the link(anchor tag), use the following jQuery code snippet.
    $("a.ajaxLink").on('click', function (e) {
        //code for the link action
        return false;
    });
    */

    //Now to get the ajax content and display it and change the browser URL to the specific location without refresh use the following code.
    $("a.ajaxLink").on('click', function (e) {
        e.preventDefault();
        /*
        - if uncomment the ABOVE line, html5 nonsupported browers won't change the url but will display the ajax content;
        - if commented, html5 nonsupported browers will reload the page to the specified link.
        */

        //get the link location that was clicked
        pageurl = $(this).attr('href');

        //to get the ajax content and display in div #content-main
        $('#content-main').load(pageurl+'?rel=tab'+'#content-main');

        //to change the browser URL to the given link location
        if(pageurl!=window.location){
            window.history.pushState({path:pageurl},'',pageurl);
        }
        //stop refreshing to the page given in
        return false;
    });

    //the below code is to override back button to get the ajax content without page reload
    //added some details from http://rosspenman.com/pushstate-jquery/ - otherwise going back in history to initial page resulted in issues
    $(window).on('popstate', function(e) {
        if (e.originalEvent.state !== null) {
            $('#content-main').load(location.pathname+'?rel=tab'+'#content-main');
        }
        else {
            $('body').load(location.pathname+'?rel=tab'+'#content-main');
        }
    });
});
Was it helpful?

Solution

Following hawaii's post

$(document).ready(function() {
  $("body").css("backgroundImage", "url(backgroundImageUrl)");
  $.ajax().done(function() {
    return $("body").css("backgroundImage", "");
  });
});

Edit

*EDIT: I add something which I hope helps a bit for clarification: my site (index.html), simplyfied, looks like this:
next

        <div id="content-main">
            <div id="othercontent"></div> //<== this is content from other html-file
        </div>
</body> The User clicks on the link, and then ,using jquery load(), some html-file content gets loaded into the div

(#content-main), and in the end everything only happens in this single index.html. So if I assign a background img to the body of the index.html (using jquery $('body').css...), I need to find a way to identify when other content exists in the DIV #content-main..*

hmm, this does not work. When I click on a link on my page, the DIV get's filled with new content, but as I'm still on the index.html, the body background does not change. – JWatan

.ready() appears fires "once". See http://api.jquery.com/ready/

At original post , 1st paragraph

ONLY on the initially loading page (i.e. in the body-tag), but as soon as some (other) content gets loaded into that "ajax" DIV, this background should disappear from index.html. – JWatan

body's css backgroundImage property should only be called "once". As described at 1st paragraph of question, the entire page does not appear to "reload", only a descendant of body element, that is, div.

An if statement is not necessarily needed to check the content of the dynamically populated div (descendant of body). It could be included, but is not necessary, needed to fulfill the requirement, of the body element's backgroundImage addition or removal.

The above piece should work to fulfill the original requirement, without including further tests

jsfiddle http://jsfiddle.net/guest271314/t6ZYf/

If requirement inlcudes other than loading backgroundImage once , please clarify ? Happy to help, if able

Note

The .done() http://api.jquery.com/deferred.done/ should be called after the completed $.ajax() request.

Edit (updated to reflect original question revisions https://stackoverflow.com/posts/22673564/revisions)

$(function () { /* `document` `ready` */
  $("body").css("backgroundImage", "url(backgroundImageUrl)"); /* set css`backgroundImage` property for `body` "once", at `document` `ready`, "initial page load", please see jquery `.ready()` api.jquery.com/ready */
//AJAX: load html-pages dynamically into this site's div (#content-main), and add browser history and URL support

    /*unnecessary? I don't use the next 3 lines of code
    //To override the default action for the link(anchor tag), use the following jQuery code snippet.
    $("a.ajaxLink").on('click', function (e) {
        //code for the link action
        return false;
    });
    */

    //Now to get the ajax content and display it and change the browser URL to the specific location without refresh use the following code.
    $("a.ajaxLink").on('click', function (e) {
        e.preventDefault();
        /*
        - if uncomment the ABOVE line, html5 nonsupported browers won't change the url but will display the ajax content;
        - if commented, html5 nonsupported browers will reload the page to the specified link.
        */

        //get the link location that was clicked
        pageurl = $(this).attr('href');

        //to get the ajax content and display in div #content-main
        $('#content-main').load(pageurl+'?rel=tab'+'#content-main', function() {
        return $("body").css("backgroundImage", ""); /* jquery `.load()` `complete` `callback`, remove `backgroundImage` from `body` */
        });

        //to change the browser URL to the given link location
        if(pageurl!=window.location){
            window.history.pushState({path:pageurl},'',pageurl);
        }
        //stop refreshing to the page given in
        return false;
    });

    //the below code is to override back button to get the ajax content without page reload
    //added some details from http://rosspenman.com/pushstate-jquery/ - otherwise going back in history to initial page resulted in issues
    $(window).on('popstate', function(e) {
        if (e.originalEvent.state !== null) {
            $('#content-main').load(location.pathname+'?rel=tab'+'#content-main', function() {
        return ($("body").css("backgroundImage") != "" ? $("body").css("backgroundImage", "") : null); /* jquery `.load()` `complete` `callback`, `if` `backgroundImage` not empty string, remove `backgroundImage`, `else` return `null` */
        })
        }
        else { /* not certain here, if needed to achieve requirement, Try place `load()` `callback` at `if` statement above at `load()` `callback` here, as well */
            $('body').load(location.pathname+'?rel=tab'+'#content-main');
        }
    });
});

Note see jquery .load() complete callback http://api.jquery.com/load/

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top