简体   繁体   English

如何根据URL数据显示数据?

[英]How do I get my data to display based on URL data?

I'm trying to get some dynamic content to display based on UTMs in my URL. 我正在尝试根据我的URL中的UTM显示一些动态内容。 While I can call the function in the browser to get the content to display, it isn't doing so on page load. 虽然我可以在浏览器中调用该函数来显示内容,但在页面加载时却没有这样做。 (Function works, but my timing must be off) (功能有效,但我的时间必须关闭)

I've tried to change the order in which I call jQuery and my JS file, but either way it doesn't show unless I paste my function into chrome dev tools. 我试图改变我调用jQuery和我的JS文件的顺序,但除非我将我的函数粘贴到chrome dev工具中,否则它不会显示。

Here's the relevant part of the function: 这是函数的相关部分:

// regex to find the URL param above
var dynamicContent = getParameterByName('dc');

 $(document).ready(function() {

    if (dynamicContent == 'fintech') {
        $('#fintech').show();
    } 
    else if (dynamicContent == 'martech') {
        $('#martech').show();
    } 
//... excluded remaining options
    else {
        $('#default-content').show();
    }

And the HTML: 和HTML:

<span id="default-content" class="dynamic-content">Tech</span>
<span id="fintech" class="dynamic-content">Fintech</span>
<span id="martech" class="dynamic-content">Martech</span>
<!-- excluded remaining options -->

And here's my header in case there's a different way I should be calling everything: 这是我的标题,以防我应该调用一切的不同方式:

<head>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="assets/css/main.css" />
<noscript><link rel="stylesheet" href="assets/css/noscript.css" /></noscript>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="assets/dynamic.js" type="text/javascript"></script>
</head>

Again, as copying/pasting my code into dev tools after the page load works, how do I time it so that this function runs on page load? 同样,在页面加载工作后将我的代码复制/粘贴到开发工具中时,如何计算它以使该函数在页面加载时运行?

Thanks. 谢谢。

Maybe try this: 也许试试这个:

$(window).on('load', function () {
    var dynamicContent = getParameterByName('dc');

    if (dynamicContent == 'fintech') {
        $('#fintech').show();
    }
    else if (dynamicContent == 'martech') {
        $('#martech').show();
    }
    //... excluded remaining options
    else {
        $('#default-content').show();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据 Node JS 发送的 JSON 数据在我的 HTML 页面上显示图表 - How do I display chart on my HTML page based on JSON data sent by Node JS 如何使用 JavaScript 让我的数据显示在 flash 卡上? - How do I get my data to display on flash cards using JavaScript? 如何同时从多个 api 获取数据并将它们显示在我的网页上? - How do i get data from multiple apis simultanously and display them on my webpage? PHP:如何基于值从Java语言中的SQL获取数据? - PHP: How do I can get data from SQL in my Javascript based on a value? 我如何将我的json解码数据动态显示到ul中 - How do i dynamically display my json decoded data into a ul 如何使用Chartkick显示数据? - How can I get my data to display using Chartkick? 如何让我的表格正确显示分组数据? - How can I get my table to display grouped data properly? 如何使页面显示函数完成时而不是每个函数完成时处理的数据 - How do I get my page to display data that my functions process as they finish rather than when every function finishes 如何从 Express 中的 API 获取要在 Angular 中显示的数据? - How do I get data to display in Angular from API in Express? 如何在 mongodb 中获取和显示引用的数据? - how do I get and display the referenced data in mongodb?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM