Solved

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

Posted on 2011-03-02
4
517 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now