0

I am working on a sharepoint online team site collection. and i have a discussion board list. now inside the AllItems.aspx view, i added the following script:-

<script src="/assets/jquery-1.11.3.js" type="text/javascript"></script>
<script>
setTimeout(function() {
$('a[href*="mydiscussionboard"]').attr('target', '_blank');
}, 3000);

</script>

the purpose of the script is that whenever a user clicks on a discussion board item, the item should open in a new tab instead of opening it on the current tab. this will ease reading multiple items. and i had to define my script inside a counter otherwise the script will not work. seems the 3 seconds can ensure that the page is fully loaded. Now the script might not be the best thing to do !! but at-least it worked well, even when i embed the allitems view inside my modern page, the script worked.

But the only issue i am facing is that if the users do a paging on the items, or click on the load icon, or click on one of the tabs (such as "my discussions" or "what's hot") as follow:-

enter image description here

then the items' links will not open in new tabs.. so my question is how i can force my script to run after doing paging or partial updates??

  • 1
    Use event listeners to tell when the items are loaded instead of using timeout. You can also listen for when the list is paged, tabbed or refreshed. And lastly, I would avoid dictating browser behaviour for your users. – wjervis Apr 28 '18 at 15:43
  • @wjervis thanks for your reply.. but can i know exactly why u mentioned this I would avoid dictating browser behaviour for your users ?? now in my case i want to add the list view inside my modern page using the embed web part and using IsDlg=1. and if i do not modify the default behavioure when users click on the list items, then on the normal scenario, when a user click on the item link inside the embed web part, the whole page will be shown inside the web part without ` IsDlg=1.`. so i want to force the item to open in a separate tab.. – john Gu Apr 29 '18 at 1:41
  • 1
    I personally dislike when a link opens in a new tab, since I already know how to make a link open in a new tab. target=_blank forces people to open in a new tab, regardless if they wanted to or not. Anyhow, let's see if I got this straight. You have a modern page with embed web part. This web part contains another page that has the list view that you are affecting. When they click a link in that list view, it opens within the embed web part. Is that correct? – wjervis Apr 29 '18 at 1:46
  • @wjervis yes exactly. now when i embed the list view page inside my modern page, i added the following IsDlg=1 to my iframe url.. this will remove the unwanted components from the embedded page .. but if the user click on an item inside the embedded page, a new page will open within the embed web part, but the new page will not have IsDlg=1 , so all the page's components such as header, left side navigation will be shown inside the embed web part, which looks totally wrong..so i want the item to open in new tab... – john Gu Apr 29 '18 at 16:45
2

Two things, to avoid the three second delay, use one of the several techniques to ensure your code runs after the page is fully loaded.

One technique: Wrap your code in a function and then add the function name to an array of functions that SharePoint will call after loading the page.

function MyFunction() { doSomeWork... }
_spBodyOnLoadFunctionNames.push("MyFunction");

As far as your code not running after paging, by default, pages are loaded as asynchronous partial page loads. Try disabling the Minimal Download site feature.

  • _spBodyOnLoadFunctionNames.push worked well, so i removed the 3 seconds deploy. thanks for this..now regarding the partial load, i do not want to disable the Minimal Download site feature.. as it improves the users' interactions.. but i want to find a way to force my scrip to keep running after doing a partial load.. – john Gu Apr 29 '18 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.