Solved

.load() with VAR not working! PLEASE HELP!

Posted on 2011-03-02
4
520 Views
Last Modified: 2013-11-05
Please can someone help with this simple problem!

In this example, only the contents of a div called #business-services-info should be loaded in from the what-cms.php page, not the full page.

Can anyone explain why the .load() event is ignoring my var (ahash) as the target div location to fetch the data from?

http://ag-chartered-accountants.com/dynamic/page3.php#business-services-info

The code:

<body>

<script type="text/javascript">

$(window).load (function() {
var ahash = new String(window.location.hash);
if ("ahash:contains('#')")
 {
 $('#stuff').load('what-cms.php' + ahash);
alert("jQuery should now load the contents of the DIV called " + ahash + ", from the page what-cms.php in to the below DIV called stuff");
}
 }
 else {
  $('#stuff').load('what-cms.php #what-info');
      alert("No hash to load so defult data will be loaded");
 }
});
</script>

<div><a href="#business-services-info">my div</a></div>
<div id="stuff">Loading...</div>

</body>

Open in new window

0
Comment
Question by:INeedHelp
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 35018616
replace line 7 by :
 if( ahash != "" )

Open in new window

or if you prefer :
 if( ahash.indexOf('#') == 0)

Open in new window

0
 
LVL 1

Author Comment

by:INeedHelp
ID: 35019145
Many thanks for your reply. However this is not the problem.

The problem I need solving, is getting the jQuery .load() event to accept my variable (ahash) as the reference to the required div to load in.

So if the url in the address bar ends in #business-services-info, the jQuery .load() event will use this as the target div.

Code to get the hash part of the url:

var ahash = new String(window.location.hash);

Code to load data using the ahash variable as the refernce to the required div to be imported:

$('#stuff').load('what-cms.php' + ahash);


Problem:

The div request part of the .load() event is ignored and the whole page is being wrongly loaded in


Thanks :o)
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35019295
Try (put a space after what-cms.php) : $('#stuff').load('what-cms.php ' + ahash);
0
 
LVL 1

Author Comment

by:INeedHelp
ID: 35020520
Wow, fantastic, that works. It's amazing how smalls things like that can make all the difference.

Many thanks :o)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running a javascript project in WebStorm 2 29
message Alert on an empty search 10 23
send email part1 9 28
Send form to asp server side 6 14
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question