Solved

making multiple url calls on the same page in javascript

Posted on 2010-08-26
6
335 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

708 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

13 Experts available now in Live!

Get 1:1 Help Now