2012-08-03 58 views
8

我在一個iframe中包裝剃鬚刀視圖。剃刀視圖是不同域上的Web服務。剃刀CSS文件位置作爲變量

下面是我在做什麼:

<!DOCTYPE html> 
<html> 
<body> 

<p align="center"> 
    <img src="http://somewhere.com/images/double2.jpg" /> 
</p> 

<p align="center"> 
<iframe src="https://secure.somewhereelse.com/MyPortal?CorpID=12334D-4C12-450D-ACB1-7372B9D17C22" width="550" height="600" style="float:middle"> 
    <p>Your browser does not support iframes.</p> 
</iframe> 
</p> 
</body> 
</html> 

這是SRC網站的標題:我希望IFRAME SRC使用調用網站的CSS

<!DOCTYPE html> 
<html> 
<head> 
    <title>@ViewBag.Title</title> 
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
    <link href="@Url.Content("~/Content/themes/cupertino/jquery-ui-1.8.21.custom.css")" rel="stylesheet" type="text/css" /> 
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script> 
</head> 

有沒有辦法通過CSS URL或讓它繼承調用網站的CSS?

我甚至會解決css文件位置是從原始站點傳入的參數。

任何人有任何建議嗎?

+2

重複的SO問題:[CSS替代iframe中的內容的主體樣式](http://stackoverflow.com/questions/6494721/css-overide-body-style-for-content-in-iframe)。 – 2012-08-07 01:38:43

+0

爲什麼不在服務器端包含Web服務結果? – Gaurav 2012-08-07 04:34:56

回答

5

我會通過CSS URL作爲參數傳遞給iframesrc屬性:

<iframe src="http://somedomain.com/[email protected](ResolveStyleUrl())"></iframe> 

ResolveStyleUrl可能被定義爲:

@functions { 

    public IHtmlString ResolveStyleUrl() 
    { 
    string url = Url.Content("~/Content/site.css"); 
    string host = "http" + (Request.IsSecureConnection ? "s" : "") + "//" + Request.Url.Host + url; 
    return Raw(url); 
    } 

} 

當然,這是假設域會接受樣式url查詢字符串並在遠程頁面上顯示相應的<link />

6

您無法使用iframe在您的網站上執行您的CSS。 CSS必須包含在iframe中包含的頁面的源代碼中。它曾經是可能的,但在某些情況下使用JavaScript,並使頁面在同一個域。

您可能能夠使用自己的CSS唯一的另一種方式是如果Web服務允許您傳遞CSS的URL。但是您必須查閱Web服務的文檔才能找到答案。

5

Eroc,我很抱歉,使用iframe,你不能強制你對別人的網站的CSS,因爲大多數瀏覽器會給像一個鍍鉻的錯誤給出:

不安全的JavaScript嘗試與URL http://terenceford.com/catalog/index.php訪問框架?從網址http://www.example.com/example.php。域,協議和端口必須匹配。

但是,這並不意味着你不能從該頁面(這可以被修改,按您輕鬆



http://php.net/manual/en/book.curl.php可用於現場拆解提取HTML with http://simplehtmldom.sourceforge.net/

First玩這些功能:

curl_init(); 
curl_setopt(); 
curl_exec(); 
curl_close(); 

然後解析html。

在嘗試自己之後,您可以查看下面我爲解析beemp3內容而創建的下面這個示例,當時我想創建一個用於直接下載歌曲的豐富工具,但不幸的是,由於驗證碼的原因,你


目錄結構

C:\ WAMP \ WWW \嘗試

- simple_html_dom.php

- try.php


try.php:

<?php 
/*integrate results for dif websites seperately*/ 
require_once('simple_html_dom.php'); 
$q='eminem'; 
$mp3sites=array('http://www.beemp3.com/'); 
$ch=curl_init("{$mp3sites[0]}index.php?q={$q}&st=all"); 
curl_setopt($ch,CURLOPT_HEADER,0); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
$result=curl_exec($ch); 
curl_close($ch); 
$html=str_get_html("{$result}"); 
$ret = $html->find("a"); 

echo "<head><style type='text/css'>a:link,a{font-size:16px;font-weight:bold;font-family:helvetica;text-decoration:none;color:#458;}a:hover{color:#67b;text-decoration:underline;}a:visited{color:silver;}</style></head>"; 
$unik=array(null); 
foreach($ret as $link) 
{ 
$find="/(.{1,})(\.php)[?](file=.{1,})&song=(.{1,})/i"; 
$replace="$4"; 
if(preg_match("{$find}",$link->href)) 
    { 
    $unik[]=$link->href; 
    if(current($unik)===prev($unik)){unset($unik);} 
    else{ 
    echo "<a href='".$mp3sites[0].$link->href."'>".urldecode(preg_replace($find,$replace,$mp3sites[0].$link->href))."</a><br/>"; 
    }} 
} 
?> 

我知道你在PHP沒有代碼,但我覺得你有能力的翻譯代碼。看看這個: php to C# converter

我花了時間在這個問題上,因爲只有我能理解提供賞金的含義。 可能是答案似乎無關(因爲我沒有使用JavaScript或基於html的解決方案),但由於跨域問題,這是一個重要的教訓給你。我希望你在c#中找到類似的庫。祝你好運

+1

我總是喜歡你的回答和評論尼娜。另一個優雅的,開箱即用的解決方案。 – 2012-08-09 17:16:07

2

我知道實現這一目標的唯一方法是在服務器端發出HTTP請求,獲取結果並將結果交還給用戶。

最低限度,您需要從目標網站完全剝離標題以使用AJAX將內容注入到您的頁面中,或者在頁眉中注入您自己的CSS以將其放入IFRAME中。

無論哪種方式,你必須實現代理方法,它將目標網址作爲參數。

這種技術有許多缺點:

  1. 你必須做你服務器上的查詢,從而耗費大量的帶寬和CPU
  2. 你要實現代理
  3. 您不能傳輸來自用戶的域專用cookie,儘管您可以通過重寫它們來管理新的Cookie
  4. 如果您做了很多請求,您的服務器很可能會成爲目標網站上的黑名單

與麻煩相比,好處聽起來很低。