プロが教える店舗&オフィスのセキュリティ対策術

jQueryのload()を使用して外部のhtmlの特定の箇所を、別のhtmlの特定のセレクに読み込んでいます。ただ、読み込む先のURLが動的に変わるので、変数で扱えたらと考えています。
jQueryの中身の該当する部分と思われる箇所を見てみたのですが、引数で指定されているurlを変数で扱う方法などありませんでしょうか?未熟な知識で申し訳ないのですが、どなた教えていただけませんでしょうか?
jqueryのload()を使用しなくても、同じような挙動をする他のおすすめなどもあれば教えていただけると非常に助かります。
【ソース】
$(document).ready(function(){
$("#box_A").load("[URL]←(このurlを変数で扱いたい) #box_B");
});
【jQueryの中味】
jQuery.fn.extend({
// Keep a copy of the old load
_load: jQuery.fn.load,

load: function( url, params, callback ) {
if ( typeof url !== "string" )
return this._load( url );

var off = url.indexOf(" ");
if ( off >= 0 ) {
var selector = url.slice(off, url.length);
url = url.slice(0, off);
}

// Default to a GET request
var type = "GET";

// If the second parameter was provided
if ( params )
// If it's a function
if ( jQuery.isFunction( params ) ) {
// We assume that it's the callback
callback = params;
params = null;

// Otherwise, build a param string
} else if( typeof params === "object" ) {
params = jQuery.param( params );
type = "POST";
}

var self = this;

// Request the remote document
jQuery.ajax({
url: url,
type: type,
dataType: "html",
data: params,
complete: function(res, status){
// If successful, inject the HTML into all the matched elements
if ( status == "success" || status == "notmodified" )
// See if a selector was specified
self.html( selector ?
// Create a dummy div to hold the results
jQuery("<div/>")
// inject the contents of the document in, removing the scripts
// to avoid any 'Permission Denied' errors in IE
.append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))

// Locate the specified elements
.find(selector) :

// If not, just inject the full result
res.responseText );

if( callback )
self.each( callback, [res.responseText, status, res] );
}
});
return this;
},
 【中略】
});

A 回答 (1件)

質問の意味がよくわかりません。

最初から、

 jQueryの .load() のパラメーターは変数指定可能です。

構文は、
 .load( url, [ data ], [ complete(responseText, textStatus, XMLHttpRequest) ] )

 http://api.jquery.com/load/

↓のように使えます。
var hoge="xxxx.htm";
var fuga="#box_B";

$("#box_A").load(hoge+" "+fuga);
    • good
    • 0
この回答へのお礼

お礼遅くなりまして申し訳ありません。
わかりづらい内容にかかわらず丁寧なご解答ありがとうございました。
大変に参考になり、問題も解決できました。
初歩的な事で躓いてしまいましたが、これから日々勉強してまいります。

お礼日時:2010/09/04 22:15

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!