从html页面上的不同脚本访问函数或变量

时间:2017-03-02 19:28:43

标签: javascript html

实施例,

我正在尝试检查页面特定部分是否属实,或者页面上是否有关键字。我认为最好的方法是使用js函数,但无法在脚本之外访问。这是一个例子



<script type="text/javascript">

    function evaluatesomething() {
            var brandUrl = document.getElementsByClassName("brand").item(1)["href"];

            console.log(Url);

            if (brandUrl.includes("something") == true) {
                return true;
            } else {
                return false;
            }
    }

</script>
&#13;
&#13;
&#13;

如何在另一个JS函数中调用它,如下所示:

&#13;
&#13;
  <div class="navbar navbar-fixed-top">
            <div id="navbarInner" class="navbar-inner" >
                <div id="headercontainer" class="container">
                    <script type="text/javascript">
                      
                        if (evaluatesomething() == true) {
                        
                        }
                    </script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

可以在加载整个脚本和html模板时完成此调用。 例如: - 当且仅当在<html> <head> <script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script> </head> <body> <script> var w = 300, h = 300, data = [{ t: "Now", v: 50 },{ t: "is", v: 25 },{ t: "the", v: 10 },{ t: "winter", v: 30 }]; var svg = d3.select('body') .append('svg') .attr('width', w) .attr('height', h); var yScale = d3.scale.linear() .range([0, h]) .domain([60, 0]); var label = svg.selectAll("text") .data(data) .enter() .append("text") .text(function(d) { return d.t; }) .attr("transform", function(d,i){ var xText = i * (w / data.length); var yText = h - yScale(d.v); return "translate(" + xText + "," + yText + ") rotate(90)"; }) .attr("fill", "black") .attr("font-family", "sans-serif") .attr("font-size", "11px"); </script> </body> </html>

之前加载 <!DOCTYPE html> <html> <head> <title></title> <script src="http://d3js.org/d3.v2.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <style type="text/css"> #canvas { width: 300px; height: 300px; background-color: red; } </style> </head> <body> <div id="canvas" style="border:solid"></div> <script type="text/javascript"> var w = 300, h = 300; var data = { name: "root", children: [{ name: '1', size: 70 }, { name: '2', size: 70 }, { name: '3', size: 70 }, { name: '4', size: 70 }, { name: '5', size: 70 }, { name: '6', size: 70 }, { name: '7', size: 70 }, ] } jQuery.fn.rotate = function(degrees) { $(this).css({'-webkit-transform' : 'rotate('+ degrees +'deg)', '-moz-transform' : 'rotate('+ degrees +'deg)', '-ms-transform' : 'rotate('+ degrees +'deg)', 'transform' : 'rotate('+ degrees +'deg)'}); return $(this); var r = getRandomInt(0, 180); }; var canvas = d3.select("#canvas") .append("svg:svg") .attr('width', w) .attr('height', h); var nodes = d3.layout.pack() .value(function(d) { return d.size; }) .size([w, h]) .nodes(data); nodes.shift(); var yScale = d3.scale.linear() .range([0, h]) .domain([60, 0]); canvas.selectAll('circles') .data(nodes) .enter().append('svg:circle') .attr('cx', function(d) { return d.x; }) .attr('cy', function(d) { return d.y; }) .attr('r', function(d) { return d.r; }) .attr('fill', 'white') .attr('x', 0) .attr('y', 0) .attr('width', 6) .attr('height', 6); canvas.selectAll("text") .data(nodes) .enter() .append("text") .attr("x", function(d) { return d.x; }) .attr("y", function(d) { return d.y; }) .text(function(d) { return "Pomidorek"; }) .attr("text-anchor", "middle") .attr("font-family", "sans-serif") .attr("font-size", "18px") .attr("color", "black") .attr("transform", function(d,i){ var xText = i * (w / children.length); var yText = h - yScale(d.size); return "translate(" + xText + "," + yText + ") rotate(505)"; }) </script> </body> </html> 时,您才能从function A()致电function B()
function A()

为了简化这一点,您可以尝试jQuery的简单function B()函数,该函数仅在页面正确加载后执行代码(当然忽略图像)