Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-02
4
Medium Priority
?
531 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 83

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 83

Accepted Solution

by:
leakim971 earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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 the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…
Suggested Courses

782 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