درخواست راهنمایی در خصوص کادر جستجو

Qalekhani

قلعه‌خانی
سلام. وقت بخیر دوستان

من یه نمونه کد آماده برای جستجو با استفاده از Google’s AJAX Search API دارم که میاد از موتور گوگل برای جستجو در سایت مدنظر بهره می گیره.

یک نمونه از خروجی Google API به صورت زیر هست:

{ "GsearchResultClass": "GwebSearch", "unescapedUrl": "http://result.com/", "url": "http://result.com/", "visibleUrl": "tutorialzine.com", "cacheUrl": "http://www.google.com/search?q=cache:_NSLxH-cQMAJ:result.com", "title": "Result Title", "titleNoFormatting": "Result Title", "content": "Feb 16, 2010 <b>...</b> blah blah blah.."}اسکریپت من متغیر r.unescapedUrl از کد بالا رو به عنوان لینک نتیجه به کاربر نمایش میده:

function result(r){ var arr = []; // GsearchResultClass is passed by the google API switch(r.GsearchResultClass){ case 'GwebSearch': arr = [ '<div class="webResult">', '<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>', '<p>',r.content,'</p>', '<a href="',r.unescapedUrl,'" target="_blank">',r.visibleUrl,'</a>', '</div>' ]; break;حالا من میخوام یه قطعه خاص رو به این آدرس اضافه کنم. سرچ کردم گفتند از دستور زیر باید استفاده کنم ولی متاسفانه جواب نمیده و کلا نتایج جستجو بارگذاری نمیشن.

var a="r.unescapedUrl";var b="test";var d = a.substr(28, position) + b + a.substr(position);لطفا راهنمایی کنید. 

 

Goback

کاربر عضو
alert(r.unescapedUrl+'test');

<html>

<body>

<script>

r = {

"GsearchResultClass": "GwebSearch",

"unescapedUrl": "http://result.com/",

"url": "http://result.com/",

"visibleUrl": "tutorialzine.com",

"cacheUrl": "http://www.google.com/search?q=cache:_NSLxH-cQMAJ:result.com",

"title": "Result Title",

"titleNoFormatting": "Result Title",

"content": "Feb 16, 2010 <b>...</b> blah blah blah.."

};

function result(r){

var arr = [];

// GsearchResultClass is passed by the google API

switch(r.GsearchResultClass){

case 'GwebSearch':

alert(r.unescapedUrl+'test');

arr = [

'<div class="webResult">',

'<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',

'<p>',r.content,'</p>',

'<a href="',r.unescapedUrl,'" target="_blank">',r.visibleUrl,'</a>',

'</div>'

];

break;

}

}

result(r);

</script>

</html>

 

Goback

کاربر عضو
این تابع رو به جاوااسکریپت اضافه کن:

String.prototype.splice = function( idx, rem, s ) { return (this.slice(0,idx) + s + this.slice(idx + Math.abs(rem)));};بعد اینجوری استفاده کن:

alert(r.unescapedUrl.splice( 7, 0, " test " ));الان خروجی متن بالا میشه:



 

Qalekhani

قلعه‌خانی
اینجوری استفاده کن:

alert(r.unescapedUrl.splice( 7, 0, " test " ));الان خروجی متن بالا میشه:

بی نهایت ممنون از لطف و توجه شما. فقط جسارتا اینو این خروجی alert رو بخوام به صورت لینک به کار ببرم دستورش چی میشه؟ چون خروجی این ادغام نقش لینک رو بازی میکنه در کد زیر:

قسمت <a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a> رو ببینید. میخوام خروجی دستور در نقش r.unescapedUrl جایگزین شه

کد:
function result(r){		var arr = [];				// GsearchResultClass is passed by the google API		switch(r.GsearchResultClass){						case 'GwebSearch':			alert(r.unescapedUrl+'test');				arr = [					'<div class="webResult">',					'<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',					'<p>',r.content,'</p>',					'<a href="',r.unescapedUrl,'" target="_blank">',r.visibleUrl,'</a>',					'</div>'				];			break;			}}
 
آخرین ویرایش توسط مدیر:

Goback

کاربر عضو
به جای

alert(r.unescapedUrl+'test');بذار

کد:
r.unescapedUrl = r.unescapedUrl.splice( 7, 0, " test " );
 
بالا