fbueti
fbueti

Reputation: 1

JQuery - How to call a function from a different page

One page of my website (let's call it page1) has a menu that uses jquery show and hide functions to display information based on what the user clicks.

On a different page (let's call it page2) I would like a link back to page1 but instead of the menu, I want to directly display information from one of the links within that menu.

So, in more general terms, how do I create a link that calls a jquery function from a different page?

An example would be helpful but all/any help is welcome! Thanks so much!

Upvotes: 0

Views: 279

Answers (2)

Astralian
Astralian

Reputation: 275

May $.getScript function can help you?

Upvotes: 0

KellySmith
KellySmith

Reputation: 46

Web pages running in a browser exist as independent entities. Unlike an environment like Java which includes things like namespaces for importing code from different files, web pages get one initial chance to load all the code they expect to use for the duration.

Additionally, there are ways of importing/creating/modifying code during run-time because JavaScript is a dynamic language that can be arbitrarily executed. Also, JavaScript can make asynchronous network requests (AJAX) while running which allows you to pull in dynamic content.

To more specifically answer your question - you need to figure out exactly what it is you are trying to do and then apply proper software engineering practices keeping in mind that code duplication is the great enemy here. If you have JavaScript code that you want to be able to run on two different pages, then you might consider creating a shared library that both pages can load and reference. Minify/compress it, and let the browser take care of caching it for good performance.

Now if this is jQuery information that you are trying pull in, it is likely that you have code that works with the DOM. If this is the case, then you have to keep in mind that calling the function on page1 will operate on the DOM associated with page1 and not page2.

If you need that type of functionality, then perhaps what you want to have a separate page3 that just handles the portion HTML that you are trying to display. Then page1 and page2 can make AJAX calls to load that portion of HTML into their own pages. This will be dependent on a lot of things however.

Sorry this was a little abstract, but you will need to provide more specific information if you want more specific answers :)

Upvotes: 1

Related Questions