Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

making multiple url calls on the same page in javascript

Posted on 2010-08-26
6
Medium Priority
?
374 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 1000 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses how to implement server side field validation and display customized error messages to the client.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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…

618 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