如何动态获取正在运行的文件的目录 [英] How to dynamically get the directory of the file running

查看:157
本文介绍了如何动态获取正在运行的文件的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在多个目录之间切换页面,目前我的页面是正确的,但是一旦我有+300个HTML页面,我不能这样做:

I am trying to switch between pages from multiple directories, what I have currently is right but once I have +300 HTML pages, I can't have it like this:

window.addEventListener('load', leftNav, false);

var x = location.pathname;
alert(x);

function leftNav() {
  appendUl('leftNav', 'outerUL'); 
  appendLiA('outerUL', 'offers', '/Ofertas/offers.html', 'Offers');
  appendLiA('outerUL', 'mobilecarriers', '/Ofertas/mobilecarriers.html', 'Mobile Carriers');
  appendLiA('outerUL', 'affilpixeltracking', '/Ofertas/affiliatepixel.html', 'Affiliate Pixel Tracking');
  appendLiA('outerUL', 'carrierip', '/Ofertas/carrierip.html', 'Carrier IP');
  appendLiA('outerUL', 'updtconverstats', '/Ofertas/Pag1.html', 'Update Conversion Status');
  appendLiA('outerUL', 'updtconverstats2', '/Ofertas/Pag4.html', 'Update Conversions Status - S2');
  appendLiA('outerUL', 'getconvdata', '/Ofertas/Pag2.html', 'Get Conversions Data'); 
  appendLiA('outerUL', 'getconvdata2', '/Ofertas/Pag6.html', 'Get Conversion Data - S2');
  appendLiA('outerUL', 'updtconverspr', '/Ofertas/Pag3.html', 'Update Conversions P/R'); 
  appendLiA('outerUL', 'updtconverpr2', '/Ofertas/Pag5.html', 'Update Conversions P/R - S2');
  appendLiA('outerUL', 'test', '/teste/index.html', 'Test');


function appendUl(append_to_id, ul_id) {

  var ul = document.createElement('ul');
  ul.id = ul_id;

  var appendTo = document.getElementById(append_to_id);
  appendTo.appendChild(ul);
}

function appendLiA(append_to_id, li_id, a_href, a_text, i_class) {

  var a = document.createElement('a');
  a.href = a_href;
  a.textContent = a_text;

  var li = document.createElement('li');
  li.id = li_id;
  li.appendChild(a);

  var appendTo = document.getElementById(append_to_id);
  appendTo.appendChild(li);
  }      
} 

在页面的href中,例如: /Ofertas/offers.html,我希望它能够动态地获取我在开始时使用location.pathname所做的页面目录。但现在,我希望它在页面之间切换时动态插入。任何人都知道如何做到这一点?

In the href of the pages, for example: "/Ofertas/offers.html", I want it to dynamically get the directory of the page which I am sort of doing at the beginning with the location.pathname. But now, I want it to insert in that place dynamically when changing between pages. Anyone knows how to do it?

推荐答案

我找到了一个办法。以下是最终的代码:

I found a way to do it. Here's the final code:

window.addEventListener('load', leftNav, false);

function leftNav() {
  var dirs=window.location.href.split('/'),
  cdir=dirs[dirs.length-2];

  var dir_ofertas = "";
  var dir_teste = "";

  if (cdir === "Ofertas") {
    dir_teste = '../teste/'; 

  }
  else if (cdir === "teste") {
    dir_ofertas = '../Ofertas/'; 

  }

  appendUl('leftNav', 'outerUL'); 
  appendLiA('outerUL', 'offers', dir_ofertas + 'offers.html', 'Offers');
  appendLiA('outerUL', 'mobilecarriers', dir_ofertas + 'mobilecarriers.html', 'Mobile Carriers');
  appendLiA('outerUL', 'affilpixeltracking', dir_ofertas + 'affiliatepixel.html', 'Affiliate Pixel Tracking');
  appendLiA('outerUL', 'carrierip', dir_ofertas + 'carrierip.html', 'Carrier IP');
  appendLiA('outerUL', 'updtconverstats', dir_ofertas + 'UpdtConvStats.html', 'Update Conversions Status');
  appendLiA('outerUL', 'getconvdata', dir_ofertas + 'GetConvData.html', 'Get Conversions Data'); 
  appendLiA('outerUL', 'updtconverspr', dir_ofertas + 'UpdtConv.html', 'Update Conversions P/R'); 
  appendLiA('outerUL', 'iptracker', dir_ofertas + 'iptracker.html', 'IP Tracker');
  appendLiA('outerUL', 'affiliates', dir_ofertas + 'Affiliates.html', 'Affiliates');
  appendLiA('outerUL', 'updateofferwhit', dir_ofertas + 'updateofferwhit.html', 'Update Offer Whitelist');
  appendLiA('outerUL', 'updateofferwhit2', dir_ofertas + 'updateofferwhit2.html', 'Update Offer Whitelist 2');
  appendLiA('outerUL', 'updateofferwhit3', dir_ofertas + 'updateofferwhit3.html', 'Update Offer Whitelist 3');
  appendLiA('outerUL', 'notdefined', dir_ofertas + 'Pag7.html', 'Not Defined'); 
  appendLiA('outerUL', 'notdefined2', dir_ofertas + 'Pag9.html', 'Not Definied 2');
  appendLiA('outerUL', 'test', dir_teste + 'index.html', 'Test');


function appendUl(append_to_id, ul_id) {

  var ul = document.createElement('ul');
  ul.id = ul_id;

  var appendTo = document.getElementById(append_to_id);
  appendTo.appendChild(ul);
}

function appendLiA(append_to_id, li_id, a_href, a_text, i_class) {

  var a = document.createElement('a');
  a.href = a_href;
  a.textContent = a_text;

  var li = document.createElement('li');
  li.id = li_id;
  li.appendChild(a);

  var appendTo = document.getElementById(append_to_id);
  appendTo.appendChild(li);
  }      
} 

这篇关于如何动态获取正在运行的文件的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆