Solved

making multiple url calls on the same page in javascript

Posted on 2010-08-26
6
362 Views
Last Modified: 2013-11-19
The easiest way to describe this is by example. If you look below I am try to implement the same piece of javascript code twice, but one uses a different search term. On my page though only the second one works properly:

http://www.glowfishtw.com/landing_pages/blog_search_example.html

How can I get both to run properly together?

Thanks


<html>
 <head>
     <script language="javascript">
         document.domain="construction.com";
     </script>
     <script type="text/javascript" src="http://sitelife.construction.com/ver1.0/Direct/DirectProxy"></script>

 </head>
 <body>

   <script language="javascript">
         document.domain="construction.com";
     </script>
     <script type="text/javascript" src="http://sitelife.construction.com/ver1.0/Direct/DirectProxy"></script>
 </head>
 <body>
   <script language="JavaScript">
       var requestBatch = new RequestBatch();
       var serverUrl = "http://sitelife.construction.com/ver1.0/Direct/Process";
   
       var searchType = "BlogPost";  // others are Comment, ForumPost, Gallery, Photo, Video
       
       // var searchString = "Title:Pennsylvania";  // To search only the title for Penn
       var searchString = "Pennsylvania";
              
       requestBatch.AddToRequest(new SearchAction(searchType, searchString, 10, 1));
       requestBatch.BeginRequest(serverUrl, clientCallBack);

       function clientCallBack(responseBatch) {
         // console.dir(responseBatch);  // -- use this in Firefox (only) to show the responseBatch object in the console to see what data is available.
         
         var searchResults = responseBatch.Responses[0].SearchResult.SearchResults;
         var htmlString = "<h2>" + searchType + " Search Results " + "for " + searchString + ":</h2><br>";

	 if (searchType == "BlogPost") // only showing an example of BlogPost for now
	 {
           for (var i = 0; i < searchResults.length; i++)
             htmlString += "<a href=\"" + searchResults[i].Url + "\">" + searchResults[i].PostTitle + "</a> by: <a href=\"" + searchResults[i].PostAuthor.PersonaUrl + "\">" + searchResults[i].PostAuthor.DisplayName + "</a> on " + searchResults[i].PostDate + "<br>";
         }

	 document.getElementById("output_div").innerHTML = htmlString;
       }

   </script>
   <div id="output_div"></div>
  
  
   <script language="JavaScript">
       var requestBatch = new RequestBatch();
       var serverUrl = "http://sitelife.construction.com/ver1.0/Direct/Process";
   
       var searchType = "ForumPost";  // others are Comment, ForumPost, Gallery, Photo, Video
       
       // var searchString = "Title:Pennsylvania";  // To search only the title for Penn
       var searchString = "Pennsylvania";
              
       requestBatch.AddToRequest(new SearchAction(searchType, searchString, 10, 1));
       requestBatch.BeginRequest(serverUrl, clientCallBack);

       function clientCallBack(responseBatch) {
         // console.dir(responseBatch);  // -- use this in Firefox (only) to show the responseBatch object in the console to see what data is available.
         
         var searchResults2 = responseBatch.Responses[0].SearchResult.SearchResults;
         var htmlString2 = "<h2>" + searchType + " Search Results " + "for " + searchString + ":</h2><br>";

	 if (searchType == "ForumPost") // only showing an example of BlogPost for now
	 {
           for (var i = 0; i < 10; i++)
             htmlString2 += "<a href=\"" + searchResults2[i].PostUrl + "\">" + searchResults2[i].PostTitle + "</a> by: <a href=\"" + searchResults2[i].LiteUser.PersonaUrl + "\">" + searchResults2[i].LiteUser.DisplayName + "</a> on " + searchResults2[i].LastUpdated + "<br>";
         }

	 document.getElementById("output_div2").innerHTML = htmlString2;
       }

   </script>
   <div id="output_div2"></div>
   

   
 </body>
</html>

Open in new window

0
Comment
Question by:elliottbenzle
[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
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
Fr0zT earned 250 total points
ID: 33533833
I'm not sure but the comment on lines 21 and 52 indicates the following are valid search terms:
Comment, ForumPost, Gallery, Photo, Video

In your second script you are using ForumPost which is valid, but in the first one you are using BlogPost which is not indicated as being vaild.  I'm basing this purly on the comment on lines 21 and 52.
0
 
LVL 4

Author Comment

by:elliottbenzle
ID: 33533874
BlogPost is actually valid. This is from an example script where BlogPost was the original term.
0
 
LVL 11

Assisted Solution

by:Rajesh Dalmia
Rajesh Dalmia earned 250 total points
ID: 33533877
may be possible that htmlString in 1st js does not have any value from the looping... try initialize with some value before the loop starts like

htmlString = "MyVal";
 if (searchType == "BlogPost") // only showing an example of BlogPost for now
       {
           for (var i = 0; i < searchResults.length; i++)
             htmlString += "<a href=\"" + searchResults[i].Url + "\">" + searchResults[i].PostTitle + "</a> by: <a href=\"" + searchResults[i].PostAuthor.PersonaUrl + "\">" + searchResults[i].PostAuthor.DisplayName + "</a> on " + searchResults[i].PostDate + "<br>";
         }


if this prints MyVal in div that means loop is not working....
0
Independent Software Vendors: 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!

 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33533899
also there's a difference in for loop in 2 js

1st case you are using the length and 2nd case it's fixed 10...

for (var i = 0; i < searchResults.length; i++)
0
 
LVL 4

Author Comment

by:elliottbenzle
ID: 33534085
The first part already has a defined part. That's why if you look at the page you see the title two times:

"ForumPost Search Results for Pennsylvania:


ForumPost Search Results for Pennsylvania:"

Oddly though the variable displays ForumPost for both, this line actually looks like this:

Searchtypevariable + " Search results for Pennsylvania"

I don't know why the value for both is coming out as "ForumPost"


Also I found that the problem is only there in IE
0
 
LVL 4

Author Closing Comment

by:elliottbenzle
ID: 33535287
Thanks guys. The problem was repeating variables. I renamed them searchType2, etc. in the second version of the code and now everything is working.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 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