2014-07-20 26 views
0

我最近將我的網站從www.unreadyanwilling.com重定向到cros.land。Javascript:在Wordpress中查找並替換Iframe URL

雖然重定向進行得很順利,但我想看看是否有辦法保留每個帖子已獲得的喜歡和推文。

我讀到,一種方法是用舊URL替換像按鈕的URL。問題是我有一個社交插件,它不允許我更改每個按鈕的單獨URL。

我看到,如果有代碼一個簡單的片斷我可以連接到我的帖子的結尾像按鈕Facebook的的iframe網址從新URL改變舊的URL

我試試看:http://cros.land/2013/11/technology-and-meditation/

我想通過它的src屬性來使用document.querySelectorAll()來查找iframe,因爲這個iframe沒有id或class。得到這個元素後,我想用舊屬性替換它。

到目前爲止,這段代碼似乎並沒有工作(我把它放在我的帖子的末尾):

<script type="text/javascript"> 
var oldInput = document.querySelectorAll("[src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fcros.land%2F2013%2F11%2Ftechnology-and-meditation%2F&amp;send=false&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65"]"); 
oldInput.src = "//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.unreadyandwilling.com%2F2013%2F11%2Ftechnology-and-meditation%2F&amp;send=false&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65"; 
</script> 

回答

1

我在troble前幾天。 jquery提供了我的解決方案,這裏是例子,只是用你的鏈接過濾它。

<script> 
jQuery(document).ready(function(){ 
     var iframe_src=jQuery("iframe[src*='http://player.vimeo.com/']").attr("src"); 
     jQuery("iframe[src*='http://player.vimeo.com/']").attr("src",iframe_src+"&autoplay=1"); 
}); 
</script> 

一個更認爲,在代碼中,還有另一種雙引號雙引號,它不會工作

<script type="text/javascript"> 
var oldInput = document.querySelectorAll("[src='//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fcros.land%2F2013%2F11%2Ftechnology-and-meditation%2F&amp;send=false&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65']"); 
oldInput.src = "//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.unreadyandwilling.com%2F2013%2F11%2Ftechnology-and-meditation%2F&amp;send=false&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65"; 
</script> 
+1

謝謝!我喜歡上面的實現,但我沒有在我的網站上使用jQuery。我想我不需要這麼簡單的操作...... –