The Twitter widget functions properly upon initial loading, but subsequently malfunctions

I have a web application built using the Play Framework that utilizes Ajax to load view html pages into a 'container' div.

$( "#container" ).load("/contactUs"); // The contactUs page contains the widget

In one of my html pages, I include a Twitter feed widget.

<div id= "twitter_div">
                <a class="twitter-timeline" href="https://twitter.com//XXXX" data-widget-id="XXXXXX">Tweets by @@XXXX</a>
            </div>

When I initially load this page with the widget into the container, the Twitter feed displays correctly. The resulting HTML looks like this:

<iframe id="twitter-widget-0" scrolling="no" frameborder="0" allowtransparency="true" class="twitter-timeline twitter-timeline-rendered" allowfullscreen="" style="border: none; max-width: 100%; min-width: 180px; width: 331px;" title="Twitter Timeline" height="380"></iframe>

However, if I load another html page into the same container div and then attempt to reload the Twitter page, it fails to render properly. For instance:

First call

$( "#container" ).load("/contactUs"); // works correctly

Then call $( "#container" ).load("/home");

And then try to load

$( "#container" ).load("/contactUs"); // doesn't work properly

The Twitter feed is still displayed as

 <div id= "twitter_div">
                <a class="twitter-timeline" href="https://twitter.com//XXXX" data-widget-id="XXXXXX">Tweets by @@XXXX</a>
            </div>

Without being converted to an iframe.

This raises the question - why does this happen?

EDIT

My contactUS page is structured as follows:

   <script> $(document).ready(function() { 
           !function(d,s,id){var js,fjs=d.getElementsByTagName(s)   [0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");

   });
 </script>
   <div id= "twitter_div">
            <a class="twitter-timeline" href="https://twitter.com//XXXX" data-widget-id="XXXXXX">Tweets by @@XXXX</a>
   </div>   

EDIT 2

To resolve this issue, I implemented Mouser's solution.

As a result, my main page now looks like this:

<html>  
<head>
    <script>
        window.twttr = (function (d,s,id) {
            var t, js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
            js.src="https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
            return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
            }(document, "script", "twitter-wjs"));
    </script>
</head>
<body>
    <div id = "container" ></div>
</body>

And my contactUS page is as follows:

 <script> $(document).ready(function() { 
      twttr.widgets.load();  
 });
 </script>
 <div id= "twitter_div">
        <a class="twitter-timeline" href="https://twitter.com//XXXX" data-widget-id="XXXXXX">Tweets by @@XXXX</a>
 </div>   

Answer №1

I've been utilizing the official Twitter loader provided below:

//TWITTER 
window.twttr = (function (d,s,id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
js.src="https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
}(document, "script", "twitter-wjs"));

Take note of the reference

window.twttr = (function (d,s,id)
. This specific code is crucial and should not be omitted from your page.

My suggestion is to integrate this loader into your primary webpage. Whenever you incorporate some external content into your designated div called container, execute the following code snippet

twttr.widgets.load( $("#container")[0] );

Following these steps, Twitter will efficiently search for elements within that container and correctly interpret them as timelines.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React Native - Issue with Chart not updating correctly when new data is added

I'm struggling to create a dynamic chart using the Victory-Native library (click here for documentation). The goal is to modify the chart after pressing the "Get Data" button, but I've encountered a problem that has me stumped after hours of att ...

Jquery issue: Property or method is not supported by this object

Implementing a tree structure on my JSP Page using jQuery has been quite challenging. The tree structure necessitates the import of several jQuery files. However, upon running the JSP page, I encountered an error with the message "Object doesn't suppo ...

Send information to MongoDB following a GET request in Node.js

When it comes to transferring data from Node's http.get() to mongoDB using mongoose, I'm encountering some challenges. I'm unsure of how to integrate the GET result into mongoose's methods. Here are a few queries regarding the process: ...

Tips on adjusting sticky behavior for responsiveness using bootstrap-4

In my project, I am utilizing angular with bootstrap. The header of the project consists of two parts and a content area. However, when the resolution is small, the header wraps. Check out the header and content on large screens View the header and conte ...

Displaying an iframe in Internet Explorer

My current setup involves a button that triggers the loading and printing of a report within an "invisible" iframe enclosed in a div. This process allows users to print the report from a different page without any visual disruption or changing pages, aside ...

"Embracing the power of dynamic CSS customization within Umbraco

I recently inherited an Umbraco system without much prior knowledge of the platform, and I am currently investigating the site's performance. The branding and styling of the site is currently managed through a "Brand" document type, allowing the site ...

website with a horizontal scroll-bar

Looking to create a horizontal scrolling website where the divs float to the right without specifying the container's width, as it may vary on different pages. Any suggestions? ...

Issues with cascading style sheets not loading properly on mobile browsers

A while ago, my website was hacked and I've been working on cleaning out the server, updating everything, changing passwords, etc. However, one problem still remains a mystery to me - why does the site load without style or images when viewed on a mob ...

What is the best way to add space between div elements with bootstrap classes?

For my project, I am utilizing bootstrap and I need to create space between divs. Here is the code I am using: <div className="container"> <div className="row"> <div className="col-lg-4 col-md-4 col-sm-12 col-12 box">a</div&g ...

Display HTML element within the <samp> tag using PHP

I'm using Bootstrap for my form control, but my input and button are not on the same line. <div class="form-group"> <label class="col-lg-3 control-label" id="title-meaning">Meaning:</label> <div c ...

What is the most efficient way to apply a single click handler instead of using multiple click handlers for the same

Check out the project I'm currently working on by following this link: The link provided above contains a list of clickable colors available on the right side. When a user clicks on a color, the images on the left side change accordingly. Below is t ...

Issue with vertical cell alignment in MUI x-data-grid persists following the latest update to version 7.2.0

After updating my MUI app to the latest npm packages version, specifically upgrading x-data-grid from 5.17.9 to 7.2.0, I encountered an issue. In my application, I utilize a grid where certain columns are populated using the renderCell property: const cel ...

What could be causing the overlap between the button and the div element?

My main wrapper div contains a content div and a button. However, I'm facing an issue where the button is overlapping with the content div instead of appearing below it. The content div is styled with the following CSS: #groupMembers { position: ...

How can the timezone setting be utilized in a datetimepicker?

I recently started using Datetimepicker for date and time input, but I'm a bit confused about the timezone option. How does it affect the data submission to a datetime datatype in MySQL and what changes can I expect when selecting back the data? Any i ...

Sending a parameter value from a blade view in Laravel to a controller: tips and tricks

What is the best way to pass the value "{{$ item-> id}}" to a method in a controller? For example, if we have show.blade.php with the value: "{{$ item-> id}}" In MyController.php, how can we utilize this value within the method: public function ...

Encountering an issue of duplicate key error when using multiple v-for loops with various keys

I've encountered an issue while working on a Python application that utilizes Vue.js. A ticket came my way with an error message stating: [Vue warn]: Duplicate keys detected: ''. This may cause an update error. (found in Root) The pro ...

Assistance with Redirecting Responses in Next.js API Routes

I'm currently working on implementing a login functionality in Next.js. I have made significant progress but I am facing an issue with redirecting to the homepage after a successful login. My setup involves using Next.js with Sanity as the headless CM ...

Are there any event triggers for History.pushstate?

My Google-fu is failing me. Consider the following code snippet: var stateObj = { state: "some state" }; history.pushState(stateObj, "page 2", "other.htm"); Does this trigger a window callback? I am aware of the following code: window.onpopstate = fun ...

Having trouble finding module: Unable to locate 'fs' - yet another hurdle with NextJS

Trying to access a JSON file located one directory above the NextJS application directory can be tricky. In a standard JavaScript setup, you might use the following code: var fs = require('fs'); var data = JSON.parse(fs.readFileSync(directory_pat ...

Learn the process of choosing and displaying all articles belonging to the logged-in user with JavaScript and MongoDB!

Using the code snippet below, I am able to retrieve all articles: getAllPost: (req, res) => { Article.find({}).limit().populate('author').then(articles => { res.render('home/AllPost',{ articles ...