Question

Execute URL from one page and return Javascript created from the second page

Asked by: ogdennews

Ok experts.  I'm stuck and I'm not even sure if there IS an answer to this.  The task I need to accomplish needs to be done all in Javascript.  I need a way for Page1 to execute Javascript that runs a second URL - Page2 (cannot be a popup).  Page2 will create new Javascript that I want to return for use on Page1.  I've found ways to execute Page2 but I cannot find any way to get those results brought back to the initial page.  

I've even tried to have my new Javascript on Page2 written out to a seperate .js file on the server and call that into Page1.  However when I do that it includes the .js file immediately, therefore it does not include the version of the file that was just created - it is the code from the last time the file was generated.  Thnx in advance for any help!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-06-02 at 08:05:27ID23450071
Tags

Javascript

Topics

JavaScript

,

Dynamic HTML (DHTML)

,

Scripting Languages

Participating Experts
2
Points
500
Comments
13

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. JS   form SUBMIT executed by JS code
    I have a form that if the window is closed by accident, i'd like to have a routine associated with onClose to execute a form SUBMIT so that the current contents can be saved in the db how to execute a SUBMIT from JS code programmatically?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: pratap_rPosted on 2008-06-02 at 09:21:14ID: 21693336

Not sure what you are trying to accomplish from your description. Let me see if I got it right

you need to include some javascript in your main page. The secondary javascript is dynamic or auto generated and you invoke this by calling a server page?

If I got it right then, you could use an iframe or AJAX to make a call to the server to retrieve the javascript/data to be included. Now if you have a javascript code just do an eval or append it to the current page's javascript collection

 

by: ogdennewsPosted on 2008-06-02 at 09:58:30ID: 21693593

Yes, the main page has content that does not change often so the page is cached to reduce hits on the DB.  The secondary page called runs a query to get banner ads to be displayed on the main page.  Once the query is ran I also create all the javascript necessary to display the ads.  That is why I need to do this in Javascript so it will still run even on a cached page.  The page is not regenerated dynamically but I need the ads to be able to do so - same content - new ads every time.  

However I am not very fluent in AJAX and do not know how to get my generated code from the secondary page back to the inital page to show my new ads.  You have the idea down - if you can provide code to achieve this I would be more then grateful!  :-)

 

by: NithyanandhanPosted on 2008-06-02 at 10:31:24ID: 21693816

Try this...

1) You can have your JavaScript in a separate .html file and in your main page, use window.open() to with option target as "_blank"
 Ex: window.open('example.html','_blank','width=0px,height=0px,menubar=no,scrollbars=no');
2) In example.html, place your JavaScript function and call the required method in page onload event.
 Ex:
 <script>
function abc()
{
....
}
 </script>
<body onload="abc()">

</body>
3)In acb(), you can access any of your main page element values(you can do both, get/set the value, for example in a input box).
 Ex.
i)you can set the value like this,
 opener.document.getElementById('mainpage-element-id').value=xyz;//xyz might be your calculated value in abc();
ii)you can get the value like this,
var xyz= opener.document.getElementById('mainpage-element-id').value

Note: Depending upon the need, you can also try with "window.location="asd.html", instead of window.open

Hope it helps,




 

by: pratap_rPosted on 2008-06-02 at 10:49:38ID: 21693927

since its banner ads the easiest way for you to do this would be to use IFRAMEs in your page. Just set the url of the IFRAME to point to your html page (which serves the banner) and you are good to go. The banner would be displayed without any problems. If you want the banner to time out, set a meta refresh on the banner html so it will refresh and fetch a new banner

another approach would be for you to insert the js file into the content page and modify the js file to suit your banner needs.. eg
<script src="banner.js" type="text/javascript"></script>... Make sure you set the caching to false on the js file, set the content to expire immediately or after some time, or you would get the same banners again and again :)

let me know if the above approaches are not good enough, I'll explain the ajax approach :)

btw which language are you using on your server side? or is it a plain HTML?

 

by: ogdennewsPosted on 2008-06-02 at 12:09:33ID: 21694668

Nithyanandhan:  thank you for you input but as I stated - a popup window is not a solution I can use.

pratap_r: I'm not sure the IFRAME solution will work for me either.  Page2 goes to the DB and gets all banner information for the entire page at once - multiple positions.  And the positions are scattered throughout the page layout.  I've already written a Javascript function that takes a single parameter (the ad location name) so that I can call these ads seperately from anywhere on the page and drop them in right where I call them.  With the IFRAME I would have to make seperate IFRAMES for each ad position which would in turn call Page2 multiple times, causing the query to be run each time.  I'd like to do it all with only one call to the DB.  That's why if I could just get the Javascript back from Page2 I would be completely set.

The site is done in ColdFusion with Fusebox but the cached pages are simply the HTML output of the executed pages.  Because it's HTML is why I need a Javascript solution.  Otherwise I could just do a cfinclude, order a pizza and call it a day!  :-)

 

by: pratap_rPosted on 2008-06-02 at 13:41:33ID: 21695497

try this solution. I'm not taking the ajax approach yet, lets see if this works out

you will need to include one hidden iframe in your static page.. and set the link for that to the page2.html (lets say).

now in your page2.html.. assuming you have queried all your db values and you have constructed your array or code itself do this at the end of the file

<script type="text/javascript">
window.parent.BannerResponse(data_or_parameters)
</script>

thats it.. now in your cached page you need to expose the method BannerResponse
<script type="text/javascript">
function BannerResponse(data_or_parameters)
{
alert(data_or_parameters); //do your thing here :)
}
</script>

now instead of the alert above you would do the ad banner posting. Here is your code for the cached content page. I'm adding the link for the IFRAME in js to prevent caching of the page2

<script>
function prepBannerLink()
{
var bannerLink = document.getElementById('bannerLink');
bannerLink.src = 'page2.html?cacheBuster='+new Date();
}
</script>
<BODY onload="prepBannerLink();">
<iframe src="" id="bannerLink"></iframe>
</BODY>
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

 

by: ogdennewsPosted on 2008-06-03 at 13:23:02ID: 21704179

Sorry pratap_r - got sidetracked by 30 other projects.  lol  I think that I may be able to make this work for me.  One other quetion though - and it is probably a really dumb one.  I'm pretty good with javascript but going from one page to another - not so good at.  If I make my variables on Page2 and send them back using your method (which, tyvm by the way!!) how do I repurpose that variable on Page1.

Ex. if I do something like:
var test = '<img src="1.jpg">';
on Page2 and send it back to Page1 - what do I do with it in order to make it a usable variable on Page1.  What Javascript do I use to write that on Page1 as "var test = '<img src="1.jpg">';" and then be able to call that variable "test" now?  LIke I said - may be a bit silly or trivial but these are the little things that I don't know about sending javascript back and forth between pages that is making me lose my hair prematurely!  :-)  Thnx again for any insight!!

 

by: pratap_rPosted on 2008-06-03 at 18:19:12ID: 21705977

thats easy.. just use the eval function

eg:
in your page2 you call the method as mentioned in my previous post
<script type="text/javascript">
window.parent.BannerResponse("var test='<img src=\"1.jpg\">';");
</script>

your bannerResponse method would be
<script type="text/javascript">
function BannerResponse(data_or_parameters)
{
eval(data_or_parameters);
alert(test); // you should see <img src="1.jpg"> in the alert
}
</script>

Have Fun!

 

by: ogdennewsPosted on 2008-06-05 at 06:06:35ID: 21718875

ok pratap_r - not as if you haven't already earned your points and more but I have one last question.  And believe me if I could give you 1,000 points I would - you've been a great help!  The question is: the functions that I have created to call the variables and display the ads where I want them....by time the variables get back to Page1 the functions to call those variables have already fired b/c they were run when the page loaded.  Any suggestions on that?  I've looked around at options to delay a function from being ran but I'm not quite sure that is my best approach - nor if that would actually work.  I can send samples of the code if that would help.  Thnx again!

 

by: pratap_rPosted on 2008-06-05 at 10:05:04ID: 21721685

you could call the functions again right after eval.. attach the code so I'll have a better idea about what you are looking for. Personally I feel that there may be a better approach to what you are trying to do.

 

by: ogdennewsPosted on 2008-06-05 at 12:06:28ID: 21722875

I have no doubt there might be a better way of going about this......but this is what came to mind so this is what I've been trying to get to work.  The suggestion box is always open!  :-)  So here is kind of an example of what I'm doing here:

Snippet 1 is an example of a variable that I am creating from my DB call and sending back from Page2 for one of the banner positions.

Snippet 2 is the function I wrote to display the banner when it is called.

Snippet 3 is an example of the code placed throughout my page.  When the function is called - it writes the variable at this location, which is made up of the anchor tag and the image location to make the ad work properly.

These all work perfectly together when I can make the DB call on the page in a more conventional way.  But when I resort to the Javascript is when I run into problems.  Since the Banner calls are individually done throughout the page - this is where I'm having the problem of them being run before the variable information is available for use.  Thus.....no banners displayed.  :-(  

---Code Snippet 1 ---
var bm_Right1 = '<a href="/page/advertisersBanners.redirect/bannerID/100.html" target="_new"><img src="/banners/100.gif" border="0" /></a> <img src="/images/spacer.gif" width="160" height="5" />';
 
 
 
---Code Snippet 2 ---
function banner_management(position) {
	if (typeof(window['bm_'+position]) != "undefined"){
		document.write(window['bm_'+position]);
	}
}
 
 
 
---Code Snippet 3 ---
<script language="javascript">
	banner_management('Right1');
</script>
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:

Select allOpen in new window

 

by: ogdennewsPosted on 2008-06-11 at 08:22:31ID: 21760995

pratap_r: I wanted to thank you for all your help.  Once I was able to get the variables back from the second page I was able to find a way to get everything working the way I wanted it to.  All I needed was a little shove in the right direction.  Thnx again and points well earned!!

 

by: ogdennewsPosted on 2008-06-11 at 08:25:46ID: 31463226

Thank you again for your help.  I couldn't have gotten that project finished without you.  I still wish I could give you 1,000 points!  :-)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...