Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

722 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