Solved

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

Posted on 2011-03-02
4
522 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
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)

751 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