Solved

making multiple url calls on the same page in javascript

Posted on 2010-08-26
6
342 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

23 Experts available now in Live!

Get 1:1 Help Now