您好,欢迎访问一九零五行业门户网

使用JQUERY Tabs插件宿主IFRAMES_jquery

必备的东西:
windows xp/vista/7/2003/2008
visual studio 2005 or 2008 (download the correct version of home site project above)
.net framework 2.0 and asp.net ajax 1.0
今天,很多浏览器提供了使用tab的能力去浏览更多的网页和网站。当然这是一个非常有用的功能来替代同时打开几个浏览器,但如果提供在一个页面中浏览多个网页也非常的不错。
例如,如果你的主页是由很多不同的有用的web工具或者站点组成,一个tab页面将可能非常的有用。使用框架集,iframe等等,都是宿主外部内容的典型方式。这些方法允许你在单个页面上宿主多个网页。 但是使他们能正确的布局却非常不容易。 更不用说去处理页面和iframe的scrollbars等问题。
这篇文章中,尝试去宿主外部数据,提供了一个基本的解决方案,利用asp.net,ajax和javascript 去处理一些遇到的麻烦。
计划
主旨是提供一个简单的宿主外部数据的方案,这个解决方案有下面这些简单的需求。
1、提供一个tab界面,方便浏览。
2、提供一个配置方法来添加tab
3、使每个tab页都能宿主配置的页面
基本的技术需要是:
1、仅当tab被选中的时候,加载外部的数据内容
2、保证纵向的scrollbars的设置成显示,而且仅当需要处理的数据溢出的时候,才显示scrollbars 。
3、保证该解决方案是能跨浏览器工作
解决方案的名字和主页面的名字都是 home site
分析
对于这个解决方案,我决定使用jquery ui tabs 来实现表格式的导航功能。我以前也使用过商业的开源的tab控件。但是jquery ui tabs 是轻量级的,实现非常地简单,而且是免费的。
除了jquery 和tab控件以及.net提供的功能,不需要其它的控件。 vs2005 将足以结合整个项目的开发环境,选择c#作为开发语言。
我将使用一个iframe的宿主网站内容,由于跨站点(又名跨域)的安全限制,使用jquery ui tabs去宿主外部网页将无法直接工作。
设计
这里有一个为客户提供视觉上的最低限度的需求:
该解决方案,将需要三种不同的功能模块:
1、配置模块
2、使用jquery ui tabs 插件的tab界面
3、使用iframe元素宿主网页内容办法。
配置模块:
一个需求的功能是是使tab可配置。 我选择最低限度,将tab的配置信息放到一个xml文件之中。虽然我可以更进一步的深入,使tab能的动态增加和删除,我决定在本文的第二篇中提供此功能。
xml文件的格式如下:
代码
复制代码 代码如下:
参数描述:
id = tab的唯一id。这个id不能包含空格
displayname =显示在tab头部的名字
path = 带查询参数的url,http://是可选的。
配置文件的名字是:tabconfig.xml。现在必须手动添加或删除tab来更新解决方案的配置文件。
内容加载器:
可以说,没有内容加载模块是需要用iframe去为tab页面设置内部的列表项。但是如果iframe在一个通过使用锚作为每个tab列表项元素的子元素的独立的宿主网页中,我觉得在功能和测试方面没有比iframe的更好的控件了:
如果你愿意,将内容装载器做成一个通用的模块,它接受查询字符串参数,以便正确设置iframe元素;参数为元素的唯一的id,和源属性值,也就是加载该网页的url。
另一个内容加载器的设计要求是,必须使该iframe负载整个页面(scrolling设置为auto)。此外,该网页body必须隐藏溢出(通过设置样式属性),以避免重复滚动条。特别是当改变浏览器的大小时。最后,滚动条的处理必须能跨浏览器。
tab界面
tab界面代码非常的简单,可以从 jquery ui tabs documentation说明文档中得到详细的演示代码。jquery ui tabs说明文档中的和我们jquery ui tabs具体的实现不同的地方在于:每个tab项锚的href指向内容加载页面,随后加载所需的网页到iframe里面。
一些额外的东西
上面的标签,我认为它很方便去用一个div来显示头,logo,甚至一些链接或菜单项。一个更好的需求,我想要头部能够折叠,能使每个标签宿主的页面能有一个最大的视觉效果。
最终的设计布局如下:
代码/开发
我们从内容加载器开始,下面是标记:
代码
复制代码 代码如下:
codebehind=contentloader.aspx.cs inherits=homesite.contentloader %>
contentloader
runat=server>
真正神奇的地方是css代码,我设置body 的margin 为0,设置overflow 为hidden。防止scrollbars 出现在页面的body上。
iframe的scrolling设置为auto,因此,如果需要滚动条,也只有iframe提供给它。因为在iframe的周围有大量不好看的空白空间,将margins 也设置为0,iframe的height和width被设置成100%,来确保网页内容占尽可能多的空间。请注意html标签中使用了literal控件。正如你将看到以下的隐藏代码, 使用literal的目的是允许后台代码注入东西到iframe元素中,它能构建了正确的querystring的id和path参数。
代码
复制代码 代码如下:
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
namespace homesite
{
///
/// content loader code behind class
///
public partial class contentloader : system.web.ui.page
{
///
/// on page load we need to capture query string parameters, construct
/// an iframe element, and inject the iframe element into our literal control
///
///
///
protected void page_load(object sender, eventargs e)
{
string id = ;
string path = ;
// validate we have valid querystring parameters
// namely id and path
if (hasvalue(request[id]) &&
hasvalue(request[path]))
{
// set our local variables
id = request[id].trim().tostring();
path = request[path].trim().tostring();
// prepend the path url with http:// if needed
if (!path.tolowerinvariant().startswith(http://))
path = http:// + path;
// construct the iframe element and set the text value of the literal control
literal1.text = id=\contentframe + id + \ +
frameborder=\0\ src=\ + path +
\>;
}
else
{
// either query parameter or both are not set or do not
// exist (not passed as request parameters)
literal1.text = an +
error occurred while attempting to load a web page.;
}
}
///
/// simple static class used to validate the value of querystring
/// parameter is not null or an empty string
///
/// the object to check
/// returns true if the object (string)
/// has a value; false otherwise.
public static bool hasvalue(object o)
{
if (o == null)
return false;
if (o is string)
{
if (((string) o).trim() == string.empty)
return false;
}
return true;
}
}
}
只要你将querystring的id和path参数传递给它,装载的页面能够单独的运行。通过vs2005浏览网页,有url的示例:http://localhost:49573/contentloader.aspx?id=1234&path=www.amazon.com.
复制代码 代码如下:
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.io;
using system.xml;
using system.text;namespace homesite
{
    ///
    /// tab configuration static handling class
    ///
    public static class tabconfiguration
    {
        ///
        /// this class returns a collection of tabdefinition classes created from
        /// parsing the tab definitions defined in the tabconfig.xml file.
        ///
        /// the page reference
        ///         calling this class
        /// arraylist of tabdefinition classes
        public static arraylist loadconfiguration(page page)
        {
            // local container for tab definitions
            arraylist tablist = new arraylist();
            try
            {
                // read the contents of the tabconfig.xml file
                streamreader reader = new streamreader(new filestream(
                   page.mappath(./tabconfig.xml),
                   filemode.open, fileaccess.read));
                string xmlcontent = reader.readtoend();
                reader.close();
                reader.dispose();
                // create an xml document and load the tab configuration file contents
                xmldocument xmldoc = new xmldocument();
                xmldoc.loadxml(xmlcontent);
                // iterate through each tab definition, create a tabdefinition class,
                // and add the tabdefinition to the local arraylist container
                foreach (xmlnode node in xmldoc.selectnodes(/configuration/tab))
                {
                    tabdefinition tab = new tabdefinition();
                    tab.id = node.attributes[id].value;
                    tab.displayname = node.attributes[displayname].value;
                    tab.path = node.attributes[path].value;
                    tablist.add(tab);
                }
            }
            catch
            {
                // do nothing
            }
            // return the tab definition
            return tablist;
        }
    }
    ///
    /// this class serves as the container for a tab definition
    ///
    public class tabdefinition
    {
        ///
        /// member variable for the unique id for the tab
        ///
        private string _id;
        ///
        /// member variable for the displayed name of the tab
        ///
        private string _displayname;
        ///
        /// member variable for the web page url to host in the tab (iframe)
        ///
        private string _path;
        ///
        /// property for the unique id for the tab
        ///
        public string id
        {
            get { return _id; }
            set { _id = value; }
        }
        ///
        /// property for the displayed name of the tab
        ///
        public string displayname
        {
            get { return _displayname; }
            set { _displayname = value; }
        }
        ///
        /// property for the web page url to host in the tab (iframe)
        ///
        public string path
        {
            get { return _path; }
            set { _path = value; }
        }
    }
}
请注意页面实例必须提供loadconfiguration方法来正确引入tabconfig.xml的位置。我本可以使用xmltextreader,但选择使用streamreader读取整个配置文件的内容和使用xmldocument对象解析tab的配置信息。因为我觉得快速转储整个配置文件比通过流程解析打开配置文件要好很多。使用xmltextreader正属于这种情况。     现在,让我们看看home site 的主页的标记
代码
复制代码 代码如下:
codebehind=default.aspx.cs inherits=homesite._default %>http://www.w3.org/1999/xhtml>
home site
              type=text/css rel=stylesheet />
              type=text/css rel=stylesheet />
2、在浏览器中,web网页被迫改变页面本身的大小,有可能跳出iframe窗体,从而取代了前父窗口。
3、我没有使用safari,opera,早期版本的ie浏览器,或任何其他浏览器的早期版本测试的此解决方案,所以要在home site中适当地调整heightadjust和widthadjust变量,适应没有测试的ie浏览器或低于ie8浏览器的版本。
总结和兴趣点
虽然这种解决方案不是很复杂,通过一个标签界面宿主外部网站内容。这是我所见过的许多网络论坛和博客要求的功能。请注意:您也可以配置标签可显示自己相关的域名或网站(在同一台服务器)。
其它类似信息

推荐信息