【问题标题】:Modify JavaScript code to use on dynamic pages修改 JavaScript 代码以在动态页面上使用
【发布时间】:2011-11-24 00:13:19
【问题描述】:

感谢您的回复。对不起,如果没有意义我会再试一次,我可能会让事情复杂化!

我有一个框架集 index.html,其中 top.php 只是广播流,下面是 index.php,它是带有导航和一切的完整 joomla 网站。

问题是,如果用户通过搜索引擎找到该网站。它将把他们带到 index.php,他们不会得到 top.php 的框架集。我在 top.php 和 index.php 中使用了这段代码:

if(self.location==top.location)self.location="index.html";

除了让用户通过搜索引擎寻找什么页面之外,它还非常有效。

所以我找到了这篇文章(在“更好的方法”部分下查看),它向您展示了如何对其进行编码,因此如果用户的内容在 about-us.html 上,它将带​​您到该页面,但仍确保它是在框架集中。 http://scriptasylum.com/tutorials/frameredirect/frameredirect.html

我想要这样的东西,但不幸的是,它是一个 Joomla 网站,我没有 page1.html、page2.html 等能够添加代码并按照他们的说明进行相应更改。我只有一页 index.php 可以“动态”动态生成页面

那么有谁知道我可以做我想做的事的方法... 框架集在http://www.housocial.com/new/index.html

只是 joomla 部分http://www.housocial.com/new/index.php

再次感谢

【问题讨论】:

  • 我已经看了三遍了,不知道你想要什么……
  • 我不知道为什么你找到的这段代码被硬编码到 page1.html.... 你为什么不能自己制作框架集?
  • 更新 window.location('someurl')。我无法完全理解你在问什么。这是我的理解。您想动态更改您的网址,然后使用该逻辑。

标签: javascript iframe redirect joomla


【解决方案1】:

您可以通过 RegExp 提取文件名(我不确定这是否是您要求的)

if(self==top)
{
 self.location="index.html?"+encodeURIComponent(location.href.match(/\w+\.html$/));
}

处理 GET 参数和锚点的更灵活的解决方案:

(
  function(url,pattern,query,hash)
  {
    var filename=location.pathname.match(pattern);
    if(filename)
    {
      url+=filename;
      if(query && location.search)
      {
        url+=location.search;
      }
      if(hash)
      {
        url+=location.hash
      }
    location.href=url;
    }
  }
)('http://www.housocial.com/new/index.php/', //target-URL
  /\w+\.html$/,       //pattern to search for
  1,                  //include QUERY_STRING
  1                   //include hash
  )

【讨论】:

  • 感谢您的回复,希望能提供更好的解释。
  • 谢谢@Dr.Molle 我试过了,但是当它应该是housocial.com/new/index.php/djs.html 时却得到了housocial.com/new/index.php/index.html?null
  • 我的解决方案是在 URL 末尾查找文件名。我猜 URL 还包含一些 GET 参数,所以 RegExp 与 href 不匹配。您需要将其与 location.pathname 进行比较。请参阅上面的更新答案。
  • 谢谢@Dr.Molle 我的Javascript知识非常非常有限。因此,如果我按照他们所说的将代码放在我的框架集 index.html 中:并且您在我的 joomla index.php 文件中的最后一个代码,这应该做我想做的事情吗?我注意到您的代码是一个函数,我需要在某处调用该函数吗?谢谢
猜你喜欢
  • 2020-03-08
  • 2020-12-31
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
  • 2022-06-18
相关资源
最近更新 更多